由于模块 http 和 https,EventSource 在 angular 6 中不工作
EventSource not working in angular 6 due to module http and https
我在 angular 6 项目中使用 EventSource。当我尝试使用 ng serve
运行 我的项目时,出现以下错误:
ERROR in ./node_modules/eventsource/lib/eventsource.js Module not
found: Error: Can't resolve 'http' in
'${MY_PATH}/node_modules/eventsource/lib'
此错误与“https”模块相同。我尝试使用 HttpClientModule
而不是 HttpModule
但它没有用。我还尝试通过 运行ning npm install eventsource
显式安装 EventSource
,但问题仍然存在。
我使用的代码:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import * as EventSource from 'eventsource';
@Injectable({
providedIn: 'root'
})
export class MyService {
private eventSource: EventSource;
constructor(private http: HttpClient) {
this.eventSource = new EventSource('/subscribe');
this.eventSource.reconnectInterval = 1;
}
// Rest of the code omitted for clarity
}
我不太了解这个包,但它看起来与 Angular 或 Angular http 模块无关。您是否阅读了文档中的 browser polyfill 部分? https://www.npmjs.com/package/eventsource
我认为这个包是为 NodeJs 创建的,但你可以在浏览器端使用它,但你需要添加一些额外的文件
我设法通过使用似乎与 angular 兼容的 event source 解决了问题。为此,运行 npm install ng-event-source
。然后,在代码中,使用 EventSourcePolyfill
对象而不是 EventSource
:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { EventSourcePolyfill } from 'ng-event-source';
@Injectable({
providedIn: 'root'
})
export class MyService {
private eventSource: EventSourcePolyfill;
constructor(private http: HttpClient) {
this.eventSource = new EventSourcePolyfill('/subscribe', { heartbeatTimeout: 1000, connectionTimeout: 1000 });
}
// Rest of the code omitted for clarity
}
尝试删除导入
import * as EventSource from 'eventsource';
我在 angular 6 项目中使用 EventSource。当我尝试使用 ng serve
运行 我的项目时,出现以下错误:
ERROR in ./node_modules/eventsource/lib/eventsource.js Module not found: Error: Can't resolve 'http' in '${MY_PATH}/node_modules/eventsource/lib'
此错误与“https”模块相同。我尝试使用 HttpClientModule
而不是 HttpModule
但它没有用。我还尝试通过 运行ning npm install eventsource
显式安装 EventSource
,但问题仍然存在。
我使用的代码:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import * as EventSource from 'eventsource';
@Injectable({
providedIn: 'root'
})
export class MyService {
private eventSource: EventSource;
constructor(private http: HttpClient) {
this.eventSource = new EventSource('/subscribe');
this.eventSource.reconnectInterval = 1;
}
// Rest of the code omitted for clarity
}
我不太了解这个包,但它看起来与 Angular 或 Angular http 模块无关。您是否阅读了文档中的 browser polyfill 部分? https://www.npmjs.com/package/eventsource
我认为这个包是为 NodeJs 创建的,但你可以在浏览器端使用它,但你需要添加一些额外的文件
我设法通过使用似乎与 angular 兼容的 event source 解决了问题。为此,运行 npm install ng-event-source
。然后,在代码中,使用 EventSourcePolyfill
对象而不是 EventSource
:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { EventSourcePolyfill } from 'ng-event-source';
@Injectable({
providedIn: 'root'
})
export class MyService {
private eventSource: EventSourcePolyfill;
constructor(private http: HttpClient) {
this.eventSource = new EventSourcePolyfill('/subscribe', { heartbeatTimeout: 1000, connectionTimeout: 1000 });
}
// Rest of the code omitted for clarity
}
尝试删除导入
import * as EventSource from 'eventsource';