我在组件中面对以下代码。
private _destroy$ = new Subject<null>();
// ...
ngOnInit() {
this.someStream$
.pipe(filter(a=> a !== null), takeUntil(this._destroy$))
.subscribe(a => { });
}
ngOnDestroy() {
this._destroy$.next();
this._destroy$.complete();
}
谷歌搜索这个问题给了我one contrasted opinion关于声明代码与命令,后者受到反应式架构的青睐。
考虑到这一点,我的问题是:与我总是接近相同案例的方式相比(用下面的代码片段说明),除了声明代码还是命令代码之外还有其他方面吗?另外,当takeUntil's
谓词完成时,如何确认第一种方法最终取消订阅流?
private _subscription: Subscription = null;
// ...
ngOnInit() {
this._subscription = this.someStream$
.pipe(filter(a=> a !== null) )
.subscribe(a => { });
}
ngOnDestroy() {
this._subscription && this._subscription.unsubscribe();
}
来自
的公寓答案 0 :(得分:1)
Apparently, the takeUntil will send a "complete" signal to the subscribing party, allowing it to complete. This causes an unsubscribe.