问题
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