我正在轮询一个RESTful端点来刷新我的在线聊天消息.我知道一个实时聊天的最佳方法是Websockets,我只是想了解RxJS如何与Angular 2一起工作.
我想每隔一秒检查一下新消息.我有以下代码:
return Rx.Observable .interval(1000) .flatMapLatest(() => this.http.get(`${AppSettings.API_ENDPOINT}/messages`)) .map(response => response.json()) .map((messages: Object[]) => { return messages.map(message => this.parseData(message)); });
但是我的Typescript transpiler返回此错误:
Property ‘flatMapLatest’ does not exist on type ‘Observable<number>’
我使用的是RxJS 5.0.0-beta.0
如果我使用merge而不是flatMapLatest,它根本不调用API.
您需要使用switchMap(),RxJS 5中没有flatMapLatest().
请参阅Migrating from RxJS 4 to 5 …虽然docs不是很清楚switchMap()…
Returns a new Observable by applying a function that you supply to each item emitted by the source Observable that returns an Observable,and then emitting the items emitted by the most recently emitted of these Observables.