EventSource
EventSource copied to clipboard
EventSourcePolyfill is not defined
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?
I am not using ionic, how show the typical export look like?
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 , great, thanks
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...
@ejerskov thanks, works great with angular 11