service – Angular2 – 在routerOnDeactivate()中停止/取消/清
发布时间:2020-12-17 07:34:30 所属栏目:安全 来源:网络整理
导读:参见英文答案 End Observable interval when route changes in Angular 21个 我是Angular2的新手,如果这是一个微不足道的问题,请道歉.当我点击不同的链接时,我似乎无法停止间隔.该组件每隔3,5s从DB表中获取数据并将其用作聊天,因此100个用户可以随时添加它.
参见英文答案 >
End Observable interval when route changes in Angular 21个
我是Angular2的新手,如果这是一个微不足道的问题,请道歉.当我点击不同的链接时,我似乎无法停止间隔.该组件每隔3,5s从DB表中获取数据并将其用作聊天,因此100个用户可以随时添加它.我不希望它一直在后台运行,所以我想我会使用routeronDeactivate()函数让它在用户位于不同的链接时停止. 我想我做错了什么.有人可以帮我吗? export class FeedComponent { public feeditems: Feed[]; public timer; constructor(private _feedService: FeedService) { } getArticlesJSON() { console.log('getArticlesJSON'); this._feedService.getFeedJSON() .subscribe( data => this.feeditems = data,err => console.log(err),() => console.log('Completed') ); } routerOnDeactivate() { // this.timer.remove(); // this.timer.dematerialize(); // clearInterval(this.timer); console.log('-- deactivate '); // console.log('-- deactivate ' + JSON.stringify(this.timer)); } routerOnActivate() { this.timer = Observable.timer(5000,3500); this.timer.subscribe(() => { this.getArticlesJSON(); }); console.log('++ activate ' + JSON.stringify(this.timer)); } } routerOnActivate() { this.timer = Observable.timer(5000,3500); this.subscription = this.timer.subscribe(() => { this.getArticlesJSON(); }); } routerOnDeactivate() { this.subscription.unsubscribe(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |