我们如何转换:
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/