我的同事问我是否需要取消订阅对话框的afterClosed() Observable。
我们在ngOnDestroy()中使用takeUntil模式来取消订阅所有Observables。
this.backEvent = fromEvent(window, 'popstate')
.pipe(
takeUntil(this.destroy$)
)
.subscribe(
() => {
this.navigationService.backClicked = true;
this.navigationService.navigateBackToDirectoryCenter();
}
);
ngOnDestroy()
:在Angular组件被销毁时调用的生命周期钩子函数。ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
那么,从 afterClosed() Observable 中取消订阅是必要的吗?
dialogRef.afterClosed().subscribe(
(data) => {
console.log(data);
}
},
);
或者?dialogRef.afterClosed()
.pipe(
takeUntil(this.destroy$)
)
.subscribe(
(data) => {
console.log(data);
},
);