我尝试使用通信服务将数据从一个组件传递到另一个组件,但在subscribe多次触发之后,最后,我尝试传递的值是null。
下面是我传递值的地方:
//all good, works fine, value is being passed
this.commService.regToken.next(this.tokenModel.value);在其他组件的constructor中,我这样做:
this.commService.regToken.subscribe( (value) => {
this.value = value;
});问题是,这个订阅触发了多次,this.value得到了正确的字符串,但在多次订阅之后,它最终成为了null。我怎样才能避免这种情况?
发布于 2018-10-24 19:39:22
如果已订阅,则取消订阅
regTokenSubscription = null;
if (this.regTokenSubscription) {
this.regTokenSubscription.unsubscribe();
}
this.regTokenSubscription = this.commService.regToken.subscribe( (value) => { this.value = value; });https://stackoverflow.com/questions/52967958
复制相似问题