给出以下代码
function triggerAction() {
const asyncAction$ = of("value1");
asyncAction$
.clientLogin()
.pipe(
first(),
tap(val => console.log(`Test: ${val}`)),
)
.subscribe();
}
我需要取消订阅吗?以前首次使用修补的运算符时,一旦发出第一个事件,它们就会取消订阅,但是文档中没有立即清楚管道运算符等效运算符是否相同。
https://www.learnrxjs.io/operators/filtering/first.html https://rxjs-dev.firebaseapp.com/api/operators/first
答案 0 :(得分:11)
first
的RxJS 5和RxJS 6版本的工作方式相同,因此您无需取消订阅,因为它会完成链并因此触发处理处理程序。
如果您想确保可以向complete
添加tap
回调并查看是否会调用它(您也可以将其添加到subscribe
):
asyncAction$
.clientLogin()
.pipe(
first(),
tap({
next: val => console.log(`Test: ${val}`),
complete: () => console.log(`Complete`),
}),
)
.subscribe();
答案 1 :(得分:0)
当时间间隔发出第一个值时,subscription $将取消订阅。 请注意,即使AppComponent被销毁,在间隔发出值之前,subscription $也不会取消订阅。
因此,最好确保在ngOnDestroy挂钩中取消了所有操作。