简体   繁体   English

将RxJS 5 Rx.Observable.timer(3000).mapTo({id:1})转换为RxJS 6吗?

[英]Converting RxJS 5 Rx.Observable.timer(3000).mapTo({ id: 1 }) to RxJS 6?

How do we convert: 我们如何转换:

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

to RxJS 6? 到RxJS 6?

For example if we: 例如,如果我们:

 import { Observable, timer } from 'rxjs';

We still get: 我们仍然得到:

[ts] Property 'timer' does not exist on type 'typeof Observable'. [ts]属性“ timer”在“ typeof Observable”类型上不存在。

All in all I'm trying to get this example (From this tutorial) to work: 总之,我正在尝试使此示例(来自本教程)工作:

    // 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));

Using the new way to do pipeable operators we no long user the . 使用新的方法做管道运算符,我们不再使用. to chain observables but we use the pipe and pass in our operators separated by commas. 链接可观察变量,但我们使用管道并传入以逗号分隔的运算符。 Example in your scenario we would do 在您的方案示例中,我们将

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));

You can read more about pipeable operators Here 您可以在此处阅读更多关于可管道运算符的信息

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM