angular - 禁用订阅
问题描述
我需要关闭订阅才能不挂起。我是否正确断开订阅?如果转到另一个页面,请求将起作用,尽管流程组件实际上必须处于非活动状态。
ngUnsubscribe = new Subject<void>();
ngOnInit() {
setInterval(() => {
this.load();
}, 1000);
}
load() {
this._card.get().pipe(takeUntil(this.ngUnsubscribe)).subscribe(card => {
this.card = card;
})
}
ngOnDestroy() {
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
解决方案
下面的代码将取消订阅并且还应该停止计时器。希望这可以帮助。
import { Subscription } from 'rxjs/Subscription';
ngUnsubscribe: Subscription;
interval;
ngOnInit() {
this.interval = setInterval(() => {
this.load();
}, 1000);
}
load() {
this.ngUnsubscribe = this._card.get().subscribe(card => {
this.card = card;
});
}
ngOnDestroy() {
clearInterval(this.interval);
this.ngUnsubscribe.unsubscribe();
}
推荐阅读
- python - Django:即使电子邮件地址已被其他用户使用,用户也可以在自定义注册表单中注册
- c - 对于作业,我必须比较两个日期使用 C 中的结构。我不确定我的逻辑是否在这里错误
- html - HTML5/CSS3 Circle inside a circle with inner circlestick on the top of the inside of the outer circle
- python - 将不同长度的列表嵌套到熊猫数据框中
- crystal-reports - 如果组中的页数奇数,则水晶报告打印空白页
- javascript - 如何在 HTML 上显示特定的日期格式
- python - 为什么 'df.str.split(',',expand=True)' 不起作用?
- c# - WPF:在图像上查找文本位置
- php - Google Speech API - 有没有办法确定音频是否有人声?
- python - 在给定的代码中,无论我先放置哪个块,都会依次返回结果,而我放置的那个块 [继续描述]