javascript - 转换 RxJS 5 Rx.Observable.timer(3000).mapTo({ id : 1 }) to RxJS 6?

标签 javascript typescript rxjs rxjs6

我们如何转换:

Rx.Observable.timer(3000).mapTo({ id: 1 }) 

到 RxJS 6?

例如,如果我们:

 import { Observable, timer } from 'rxjs';

我们仍然得到:

[ts] Property 'timer' does not exist on type 'typeof Observable'.

总而言之,我正在尝试获取这个示例 (From this tutorial)上类:

    // Simulate HTTP requests 
    const getPostOne$ = Rx.Observable.timer(3000).mapTo({id: 1});
    const getPostTwo$ = Rx.Observable.timer(1000).mapTo({id: 2});

    Rx.Observable.concat(getPostOne$, getPostTwo$).subscribe(res => console.log(res));

最佳答案

使用新方法来执行可管道运算符,我们不再使用 . 来链接可观察量,而是使用管道并传入以逗号分隔的运算符。在您的场景中我们会做的示例

import { timer, concat } from 'rxjs'
import { mapTo } from 'rxjs/operators'

  getPostOne$ = timer(3000).pipe(mapTo({ id: 1 }));
  getPostTwo$ = timer(1000).pipe(mapTo({ id: 2 }));

  concat(getPostOne$, getPostTwo$).subscribe(res => console.log(res));

您可以阅读有关可管道运算符的更多信息 Here

希望这有帮助!

关于javascript - 转换 RxJS 5 Rx.Observable.timer(3000).mapTo({ id : 1 }) to RxJS 6?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51868852/

相关文章:

javascript - 为什么在 JavaScript 中导入模块有不同的方法?

node.js - node_modules/@types/错误(接口(interface) 'Element' 不能同时扩展类型 'ReactElement<any>)

Angular2 rxjs http.request.catch 对某些 http 错误有奇怪的行为

typescript - 如何在 typescript 中使用 rxjs6 分区?

javascript - 如何从回调中创建 Observable

javascript - 如何设计一个支持javascript插件的C++插件框架

javascript - 从 TinyMCE 内容动态填充一个 div

javascript - 无法禁用 Webstorm 检查 - "Unresolved variable of type"

javascript - 在 Canvas 上绘图表演 buggy

angularjs - Angular 和 Typescript : proper way to reference 'this'