当我更改路线并返回到订阅主题的相同路线时,每次更改路线时都会创建一个重复的侦听器。
为了阻止这种情况,我使用了主题的未订阅功能,但问题在取消订阅主题后出现。我无法再次订阅,也无法收听该活动。
socketservices.ts
:
connect(): Rx.BehaviorSubject<MessageEvent> {
// We define our observable which will observe any incoming messages
// from our socket.io server.
let observable = new Observable(observer => {
this.socket.on('message', (data) => {
observer.next(data);
})
return () => {
this.socket.disconnect();
}
});
// We define our Observer which will listen to messages
// from our other components and send messages back to our
// socket server whenever the `next()` method is called.
let observer = {
next: (data: Object) => {
console.log('sending message')
this.socket.emit('message', JSON.stringify(data));
},
};
// we return our Rx.Subject which is a combination
// of both an observer and observable.
return Rx.BehaviorSubject.create(observer, observable);
}
admincomponent.ts
:
//constructor includes public webSoc:SocketServiceService
this.subscription = this.webSoc.connect().subscribe(data=>{
if (data['response'] == "dvc_board") {
console.log('listening')
this._updatebins()
}
})