我有一个流调用服务,该服务返回一个流,该流需要该请求的原始流的信息。
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)都接收参数,因此,我无法使用原始流信息来调用生成另一个流的服务,并且我不希望使用订阅在原始流上,并在订阅中返回新流,因为这有点混乱。
有什么建议吗?
使用selector function
中的switchMap
操作:
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
操作员的更多信息,请在此处检查
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句