小编Luk*_*kas的帖子

Angular2 - 在routerOnDeactivate()中停止/取消/清除observable

我是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));
    }
}
Run Code Online (Sandbox Code Playgroud)

service observable angular

8
推荐指数
1
解决办法
2万
查看次数

标签 统计

angular ×1

observable ×1

service ×1