简体   繁体   中英

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?

For example if we:

 import { Observable, timer } from 'rxjs';

We still get:

[ts] Property 'timer' does not exist on type '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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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