javascript - RxJS 6 - 在管道中第一次使用时我需要取消订阅吗?
问题描述
给定以下代码
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
解决方案
RxJS 5 和 RxJS 6 版本的first
工作方式相同,因此您无需取消订阅,因为它完成了链并因此触发了 dispose 处理程序。
如果你想确定你可以添加complete
回调tap
,看看它是否被调用(你也可以添加它subscribe
):
asyncAction$
.clientLogin()
.pipe(
first(),
tap({
next: val => console.log(`Test: ${val}`),
complete: () => console.log(`Complete`),
}),
)
.subscribe();
推荐阅读
- sql - 用于将代码直接从表单插入到 SQL 中的链接表的访问按钮
- azure - Azure AD 用户使用 SOAP 与自定义用户管理同步
- azure - 我可以在 azure 中查看我的 Web 应用程序的控制台日志吗?
- angular - 如果复选框被隐藏,如何禁用复选框
- kotlin - 使用 Kotlin 扩展用 Groovy 编写的 Gradle 任务
- php - Doctrine ORM:聚合值的类型提示
- r - 在 ggplot 中向条形图添加重要性星 - 通过线连接
- sql - 编译错误:找不到方法或数据成员
- excel - 从数组中删除不包含值的整列
- laravel - 多对多多态关系中的自定义透视列名称?