加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

anglejs – 带RxJS的角度2轮询

发布时间:2020-12-17 09:17:38 所属栏目:安全 来源:网络整理
导读:我正在轮询一个RESTful端点来刷新我的在线聊天消息.我知道一个实时聊天的最佳方法是Websockets,我只是想了解RxJS如何与Angular 2一起工作. 我想每隔一秒检查一下新消息.我有以下代码: return Rx.Observable .interval(1000) .flatMapLatest(() = this.http.
我正在轮询一个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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读