当我尝试像这样进行 http 调用时:
return this.http.get(this.url).map(res => res.json());
一切都是预期的,我有正确的响应,没有错误,但是当我尝试使用间隔(通过 RxJS 运算符间隔)进行 http 调用时,我遇到了错误。
我的代码看起来:
return Observable.interval(1000).map(() => {
return this.http.get(this.url).map(res => res.json());
});
错误:
ZoneAwareError {__zone_symbol__error: Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:3000/node_modules/rxjs Error: …, __zone_symbol__stack: "(SystemJS) XHR error (404 Not Found) loading http:…alhost:3000/app/home/components/home.component.js", originalErr: ZoneAwareError}
最佳答案
您必须使用.flatMap()
和 IntervalObservable 来从另一个 Observable 获取数据:
return IntervalObservable
.create(1000)
.flatMap(() => {
return this.http.get(this.url).map(res => res.json());
});
关于javascript - 通过 Rxjs 间隔运算符进行 Angular 2 HTTP 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42072683/