Angular2-在routerOnDeactivate()中可观察到的停止/取消/清除

卢卡斯C

我是Angular2的新手,如果这是一个小问题,我们深表歉意。当我单击其他链接时,似乎无法停止间隔。该组件每隔3,5s从数据库表中获取数据,并用作聊天,因此随时可以向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));
    }
}
贡特·佐赫鲍尔(GünterZöchbauer)
routerOnActivate() {
  this.timer = Observable.timer(5000,3500);
  this.subscription = this.timer.subscribe(() => {
    this.getArticlesJSON();
  });
}

routerOnDeactivate() {
  this.subscription.unsubscribe();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章