【问题标题】:Unsubscribing to rxjs observable using takeUntil and combineLatest not working使用 takeUntil 和 combineLatest 取消订阅可观察的 rxjs 不起作用
【发布时间】:2018-11-05 04:44:36
【问题描述】:

我正在使用accepted pattern 取消订阅我的订阅:

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   
})

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

【问题讨论】:

    标签: angular rxjs observable subscription


    【解决方案1】:

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

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

    【讨论】:

    • 是的。最后返回的 observable 是最先订阅的,所以takeUntilpipe 中应该尽可能晚,在subscribe 链中应该尽可能早。 +1。
    • 刚刚测试了交换运算符,它按预期工作!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2018-07-15
    • 2018-11-22
    • 2019-04-06
    • 2023-03-24
    • 2018-01-19
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多