Angular 2 and RxJS

怎甘沉沦 提交于 2019-12-25 07:59:57

问题


I am trying to build an example realtime app with Angular 2.0.0RC5 and RxJS 5.0.0-beta.6. Although I it is all working with this code:

import {IntervalObservable} from 'rxjs/observable/IntervalObservable';
    ...
return IntervalObservable.create(1000)
                .flatMap(() => this.http.get('filename.json'))
                .map(this.extractDataCallBack)
                .catch(this.handleError);

Is this the correct way? The current RxJS api docs seem to specify different methods. Is there a way to upgrade to the latest version of RxJS without breaking everything?

What happened to the Observable.interval() function? I have seen several examples using this.

Anything you can offer to shine a bit of light onto this would be great.

Many thanks

JT


回答1:


Observable.interval is just a shortcut to IntervalObservable.create:

https://github.com/ReactiveX/rxjs/blob/master/src/observable/interval.ts




回答2:


Thanks Guys, so this worked.

import { Observable }   from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/concat';
import {IntervalObservable} from 'rxjs/observable/IntervalObservable';

...

private serverGetRequestContinuous(requestInterval: number, jsonFileName: String): Observable<any> {
        return Observable.concat(Observable.of(null), IntervalObservable.create(10000))
            .flatMap(() => this.http.get('filename.json'))
            .map(this.extractDataCallBack)
            .catch(this.handleError);
    }

By concatenatinge these two Observables there is no delay for the first request. If IntervalObservable.create(requestInterval) was called alone, the polling would start only after the specified requestInterval. This first Observable causes a single request right now and thus no delay in displaying data in the UI. The second Observable polls every 10 seconds.



来源:https://stackoverflow.com/questions/39053095/angular-2-and-rxjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!