我有一个流调用一个服务,该服务返回一个流,该流需要请求的原始流的信息.
this.messages$= this.selectedChat$
.switchMap(chat => this.chatService.subscribeMessages(chat.room))
.map(messages) =>
messages.map((message: any) => {
//Here need add information 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 information
return message;
})
);
有关switchMap运算符的更多信息,请检查here
原文地址:https://codeday.me/bug/20190727/1550856.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。