【发布时间】:2018-04-19 13:10:43
【问题描述】:
我目前想知道角度订阅和取消订阅。关于这个主题有很多东西,所以我有点迷失了。
我应该什么时候退订?如果我永远不取消订阅会发生什么?我从未遇到过 reliquat 订阅的任何错误。
有没有办法自动取消订阅组件/应用程序中的所有内容,这样我就不必通过订阅声明 1 个属性?这可能很烦人:
@Component({
selector: 'subscriptionTest',
template: `...`,
})
export class SubTestComponent {
first;
second;
third;
constructor(private remote: RemoteService){}
ngOnInit() {
this.first = remote.getSomeData().subscribe(data => // do something);
this.second = Observable.interval(500).subscribe(event => // do something);
this.third = remote.getSomeOtherData().subscribe(data => // do something);
}
ngOnDestroy() {
this.first.unsubscribe();
this.second.unsubscribe();
this.third.unsubscribe();
}
}
【问题讨论】:
-
stackoverflow.com/questions/38008334/… 这可能会有所帮助。这也是另一篇关于如何以及何时取消订阅的 gud 文章。 medium.com/@benlesh/rxjs-dont-unsubscribe-6753ed4fda87
标签: angular typescript