昨天我把应用程序从Angular5迁移到Angular7。在这个过程中,我已经按照运行应用程序时的建议将eventsource更改为eventsourcepolyfill。但从那以后我面临着一个奇怪的问题。这就是我在控制台中得到的问题,如下所示:
ERROR TypeError: Cannot read property 'heartbeatTimeout' of undefined
at new EventSourcePolyfill (eventsource.js:155)
at AppFooterComponent.push../src/app/layout/footer/footer.component.ts.AppFooterComponent.connect (footer.component.ts:133)
at SafeSubscriber._next (footer.component.ts:52)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub (Subscriber.js:196)
at SafeSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next (Subscriber.js:134)
at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next (Subscriber.js:77)
at Subscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at AsyncAction.dispatch (timer.js:31)
at AsyncAction.push../node_modules/rxjs/_esm5/internal/scheduler/AsyncAction.js.AsyncAction._execute (AsyncAction.js:63)
at AsyncAction.push../node_modules/rxjs/_esm5/internal/scheduler/AsyncAction.js.AsyncAction.execute (AsyncAction.js:51)
这就是我在代码中所做的:
import { EventSourcePolyfill } from 'ng-event-source';
let source = new EventSourcePolyfill('/api/v1/events/register');
有人能建议我如何解决这个问题吗?
谢谢。
最佳答案
您必须传递其他参数,如
eventSource = new EventSourcePolyfill(url,
{ heartbeatTimeout: 5000, connectionTimeout: 5000, headers: { 'Authorization': 'Basic foo' }});