javascript - 通过 Rxjs 间隔运算符进行 Angular 2 HTTP 调用

标签 javascript angular typescript rxjs

当我尝试像这样进行 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/

相关文章:

html - Angular 7 组件 : how to detect a change by user interaction?

cordova - 如何绕过 'Cordova is not defined' 并继续使用 Ionic Lab

javascript - 列表中的每个子项都应该有一个唯一的 "key"prop : Unable to determine the issue in my code

javascript - AES文件上传加密/解密及使用

javascript - 使用 WinJS 在 FlipView 中显示 PageControl

angularjs - ngUpgrade - 从 ng2 路由到 ng1 - 怎么样?

angular - 如何替换 Ionic 中的当前页面?

javascript - 将数组项输出为 HTML

javascript - 如何更改 Three.js 中几何体的基本参数(例如半径、顶点数等)

angular - 使用动态导入延迟加载组件时编译器发出警告