EventSource icon indicating copy to clipboard operation
EventSource copied to clipboard

EventSourcePolyfill is not defined

Open ejerskov opened this issue 5 years ago • 5 comments

Im working in ionic 4.10.2 with angular 7.3.5.

npm install event-source-polyfill

Then added this to the top of my file:

import 'event-source-polyfill/src/eventsource.js' declare var EventSourcePolyfill: any;

And then using it like this:

    this.source = new EventSourcePolyfill(url, {
      headers: {
        "Authorization": "Bearer " + this.userService.user.token,
        "X-Requested-With": "XMLHttpRequest"
      }
    });

I then get the following error:

ERROR ReferenceError: EventSourcePolyfill is not defined

I've checked that the path is correct. Any other things Im missing?

ejerskov avatar Mar 24 '19 21:03 ejerskov

I am not using ionic, how show the typical export look like?

Yaffle avatar Mar 25 '19 06:03 Yaffle

I found the solution: import { EventSourcePolyfill } from 'event-source-polyfill/src/eventsource.min.js' The old way (in the Readme) seems not to work in newer angular projects

ejerskov avatar Mar 25 '19 16:03 ejerskov

@ejerskov , great, thanks

Yaffle avatar Mar 26 '19 22:03 Yaffle

The solution of @ejerskov didn't worked for me (Angular 8.0.1). The only way I found to make it works was to globally import the event-source-polyfill package.

import * as ssePolyfill from 'event-source-polyfill/src/eventsource.min.js';
new ssePolyfill.EventSourcePolyfill(url, opts);

However, I have no clue why this global import works while the named import didn't...

Mweydert avatar Oct 14 '19 10:10 Mweydert

@ejerskov thanks, works great with angular 11

Viktor-Bredihin avatar Mar 02 '21 15:03 Viktor-Bredihin