【发布时间】:2019-02-02 18:44:58
【问题描述】:
为了避免我的 Angular 应用程序中的内存泄漏,我使用以下众所周知的模式来取消订阅 Observables:
unsubscribe = new Subject();
ngOnInit() {
this.myService.getStuff()
.pipe(takeUntil(this.unsubscribe))
.subscribe(result => {
// processing the result
});
}
ngOnDestroy() {
this.unsubscribe.next();
}
这似乎工作正常,但在某些示例中,我注意到除了 next() 之外,complete() 上也调用了 complete():
ngOnDestroy() {
this.unsubscribe.next();
this.unsubscribe.complete(); // like this
}
这里有必要打电话给complete()吗?如果是这样,为什么?在这种情况下不调用complete()会有什么后果?
【问题讨论】:
标签: javascript angular rxjs