微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Rxjs:将两个流合并为一个依赖于另一个流的流

我有一个调用一个服务,该服务返回一个流,该流需要请求的原始流的信息.

this.messages$= this.selectedChat$
.switchMap(chat => this.chatService.subscribeMessages(chat.room))
.map(messages) =>
  messages.map((message: any) => {
    //Here need add @R_1000_4045@ion of the chat object
    return message;
  })
);

我看到的所有运算符都可以合并流并稍后将它们分开(比如combineLatest)接收参数,因此,我无法使用原始流信息来调用生成其他流的服务,我不想使用订阅在原始流上并在订阅内返回一个新流,因为它有点乱码.

有什么建议?

解决方法:

在switchMap操作中使用selector函数

this.messages$= this.selectedChat$
.switchMap(chat => this.chatService.subscribeMessages(chat.room), 
(chat,messages)=>({chat,messages})) // create a new object, using the inner observable and the mapped one
.map(data =>
  data.messages.map((message: any) => {
    // Here data.chat has the chat @R_1000_4045@ion
    return message;
  })
);

有关switchMap运算符的更多信息,请检查here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐