简体   繁体   English

Angular 2和RxJS

[英]Angular 2 and RxJS

I am trying to build an example realtime app with Angular 2.0.0RC5 and RxJS 5.0.0-beta.6. 我正在尝试使用Angular 2.0.0RC5和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. 当前的RxJS api文档似乎指定了不同的方法。 Is there a way to upgrade to the latest version of RxJS without breaking everything? 有没有一种方法可以升级到最新版本的RxJS而又不会破坏所有功能?

What happened to the Observable.interval() function? Observable.interval()函数发生了什么? 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 日本电信

Observable.interval is just a shortcut to IntervalObservable.create : Observable.interval只是IntervalObservable.create的快捷方式:

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

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. 通过将这两个Observables连接起来,第一个请求没有延迟。 If IntervalObservable.create(requestInterval) was called alone, the polling would start only after the specified requestInterval. 如果单独调用IntervalObservable.create(requestInterval),则轮询将仅在指定的requestInterval之后开始。 This first Observable causes a single request right now and thus no delay in displaying data in the UI. 此第一个Observable立即引起单个请求,因此在UI中显示数据没有延迟。 The second Observable polls every 10 seconds. 第二个Observable每10秒轮询一次。

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

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