订阅在当时发出一个项目而不是在Rxjs中的列表

weixin_38109453 2019-09-12 03:15:25

我目前正在努力弄清楚Angular 4应用程序中Rxjs的行为。 我的代码是: this.server.get("incidents") //http get resource .flatMap((res) => res.value) //the incident array is in a property called value of the json returned .map((incident) => new Incident(incident)) // conversion from json to typed class .subscribe(i => {this.ng2TableData.push(i);}) //subscribe 在最后一行,我希望订阅方法立刻提供给我的整个列表,而不是它似乎obsevable被返回的时候一个Incident和订阅功能称为N次,因此迫使我采用push方法,而不是一次构建ng2TableData。 我该如何订阅整个列表,而不是当时的一个项目?








...全文
31 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38113164 2019-09-12
  • 打赏
  • 举报
回复

flatMap将使您的数组变为可观察的值流。你只想使用map。您可以再次使用map,使每个对象数组中为你的类的实例,像这样: this.server.get("incidents") .map(res => res.value.map(incident => new Incident(incident))) .subscribe(data => console.log(data)) // data is an array! 现在你得到你的阵列里,而不是订阅。

473

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧