Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了service – Angular2 – 在routerOnDeactivate()中停止/取消/清除observable大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > End Observable interval when route changes in Angular 21个
我是Angular2的新手,如果这是一个微不足道的问题,请道歉.当我点击不同的链接时,我似乎无法停止间隔.该组件每隔3,5s从DB表中获取数据并将其用作聊天,因此100个用户可以随时添加它.我不希望它一直在@L_801_6@运行,所以我想我会使用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();
}

大佬总结

以上是大佬教程为你收集整理的service – Angular2 – 在routerOnDeactivate()中停止/取消/清除observable全部内容,希望文章能够帮你解决service – Angular2 – 在routerOnDeactivate()中停止/取消/清除observable所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。