使用 takeUntil 和 combineLatest 取消订阅 rxjs observable 不工作

Unsubscribing to rxjs observable using takeUntil and combineLatest not working

我正在使用 取消订阅:

private ngUnsubscribe: Subject<void> = new Subject();

ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
}

但是,我在使用 takeUntilcombineLatest 的以下 rxjs 代码时遇到问题:

this.observableA$.pipe(
    takeUntil(this.ngUnsubscribe),
    combineLatest(
        this.observableB$,
        this.observableC$
    )
).subscribe(([A, B, C]) => {
    // do some work   
})

这个订阅似乎是持续存在的,因为我可以看到代码在组件被销毁并重新初始化后多次触发。任何帮助将不胜感激。

您的内部 observableB 和 observableC 没有取消订阅。尝试重新排列您的运算符:

combineLatest(
    this.observableB$,
    this.observableC$
),
takeUntil(this.ngUnsubscribe)