我正在尝试在执行所有可观察对象时触发回调。在我的另一个较旧的项目中,我像这样使用了 finally
并且效果很好:
this.myService.callDummy()
.finally(() => console.log('Works!'))
.subscribe(result => ...)
但现在我正在使用带有 Pipeable operators 的较新版本的 RxJS,但是 finally
调用(现在重命名为 finalize
)从来没有被执行。找不到什么信息,我不确定我做错了什么。
combineLatest(
this.route.queryParams,
this.myService.callDummy1(),
this.myService.callDummy2()
)
.pipe(finalize(() => console.log('Does not work!')))
.subscribe(results => ...);
感谢任何帮助。
最佳答案
在可观察对象中,触发和完成不是一回事。
即使每个项目都发出一个值,route.queryParams 根据定义永远不会完成,因为这就是 Angular 实现它的方式,作为一个非终止的可观察对象。您需要手动完成它才能执行 finalize,因为 combineLatest 只会在其中组合的每个可观察对象完成时完成。
combineLatest(
this.route.queryParams.pipe(take(1)), // take(1) will complete the observable after it has emitted one value
this.myService.callDummy1(),
this.myService.callDummy2()
)
.pipe(finalize(() => console.log('Does not work!')))
.subscribe(results => ...);
这将完成。
关于angular - 未调用 RxJS5 finalize 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49735826/