在Angular服务中,我创建了一个主题,并按如下所示返回它:
public entityPayLoad = new Subject<Entity>();
public getSubjectAsObservable() {
return this.entityPayLoad .asObservable();
}
我从两个不同的组件调用主题的下一个方法:
从比较1:
this.Service.entityPayLoad.next(this.entity1);
来自Comp 2:
this.Service.entityPayLoad.next(this.entity2);
我订阅的主题如下,在下一个方法调用中被调用两次:
this.entitiesSub = this.Service.getSubjectAsObservable().subscribe((entity) => {
this.loadNewMainEntity(selectedMainEntity);
});
最初它可以正常工作,但是在获得两个下一个方法调用之后, 对于随后的下一个请求,订户都会被呼叫两次。
我在ngondestroy中已取消订阅相同的内容。 但是由于我没有将组件留给以后的下一个调用,所以取消订阅不会被调用。