我有一个聊天服务,它从服务器接收响应并将响应设置为Observable变量。然后,在聊天组件的构造函数中,从聊天服务订阅可观察变量。但是,有时订阅组件不起作用。以下是代码的一部分:
- 聊天服务文件 -
private newChatReceived = new Subject<any>();
newChatReceived $ = this.newChatReceived.asObservable();
this.socket.on(ChatService.DATA, (message) => {
if (message.tp == "ChatService.ADD_NEW_CHAT_RESULT"){
this.newChatReceived.next(data);}
} });
- 聊天组件文件
Constructor(private chatService: ChatService){
chatService.newChatReceived$.subscribe(data => {
let rst= JSON.parse(data);
});
}