environment-variables - Observable.timer(0,10000) 取消订阅后仍然在生产构建中的其他组件上被调用
问题描述
我每 10 秒使用 Observable.timer(0,10000) 调用一个 API,并取消订阅 ngOnDestroy() 方法。但是这个调用并没有停止,它不断地添加到队列中,即使在离开该组件后也调用。
我试图破坏ngOnDestroy()
功能上的组件。但它仍然是它的接听电话。
它应该只在这个组件上调用,并且应该每 10 秒调用一次,但是如果我们去其他组件调用被添加到队列中并且一旦返回到实例组件,它每 10 秒调用多次。
代码:
this._instanceSub = Observable.timer(0,reference.INSTANCE_CALL_INTERVAL) .subscribe(() => { this.getComonentInstance(this.componentId); }); ngOnDestroy() {
if (this._instanceSub) {
this._instanceSub.unsubscribe();
}
解决方案
上面的代码必须工作我已经使用下面的代码相同
this.subject = new Subject();
ngOnInit() {
timer(0, reference.INSTANCE_CALL_INTERVAL).pipe(
takeUntil(this.subject),
).subscribe(t => this.getComonentInstance(this.componentId));
}
在 ngOndestroy 中
ngOnDestroy() {
this.subject.next();
}
推荐阅读
- git - Github 未在 Ubuntu 18.04 上加载
- azure - 有什么方法可以使用 ARM 模板完全自动化基于 QnAMaker 的 Azure Bot
- python - 多个键上的 Pandas merge_asof
- r - 识别部分字符串匹配
- uwp - 创建 UWP 应用 MS-Teams,如左窗格/选项卡 UI 控件
- python - 如何使用 selenium 和 xpath 获取这些段落的文本
- spring-boot - 为我的 Spring Boot 应用程序创建一个单独的身份验证服务器
- java - 使用 Java 流生成对象
- typescript - 如何在 vue 3 中获取路由器的参数?
- linux - 如何创建监控线程?