Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何停止通过angular2组件中的服务调用的路由更改设置间隔?_Angular - Fatal编程技术网

如何停止通过angular2组件中的服务调用的路由更改设置间隔?

如何停止通过angular2组件中的服务调用的路由更改设置间隔?,angular,Angular,我为所有套接字方法提供了一个服务 socket.service.ts * * 我在我的组件中调用该服务 order-buy.component.ts getBuyData() { this.buy_socket=this.socketservice.getBuyOrder().subscribe( data => { // console.log(data); this.temp = d

我为所有套接字方法提供了一个服务

socket.service.ts

*

*

我在我的组件中调用该服务

order-buy.component.ts

getBuyData() {
        this.buy_socket=this.socketservice.getBuyOrder().subscribe(
            data => {
                // console.log(data);
                this.temp = data;
                if (this.temp.status == 200) {

                    this.no_data_found = false;

                    this.buy_data = this.temp.data;
                    for (var i = 0; i < this.buy_data.length; i++) {
                        this.previous = 0;
                        for (var j = 0; j <= i; j++) {
                            this.previous += this.buy_data[j].quantity;
                        }
                        this.quantity.push(this.previous.toFixed(2));
                    }
                }
                else {
                    this.no_data_found = true;
                    this.no_data=this.temp.message;
                }
            },
            error => {
                console.log(error);
            }
        )
    }
    ngOnDestroy() {
        // Will clear when component is destroyed e.g. route is navigated away from.
        clearInterval(this.buy_socket);
        console.log("order-buy ngDestroy called!");
    }
getBuyData(){
this.buy_socket=this.socketservice.getBuyOrder().subscribe(
数据=>{
//控制台日志(数据);
this.temp=数据;
如果(this.temp.status==200){
this.no_data_found=false;
this.buy_data=this.temp.data;
对于(var i=0;i
但在Ngondestory()上,我的时间间隔并没有停止,而是继续执行路线更改

如果我从服务调用方法,有人能给我建议关闭时间间隔的更好方法吗


提前感谢!

您需要使用组件销毁方法取消订阅呼叫

 ngOnDestroy() {
        this.buy_socket.unsubscribe();
        console.log("order-buy ngDestroy called!");
    }

如果你认为我的答案值得的话,请把我的答案标记为接受并放弃投票advance@DharaPatel点击勾号表示您接受我的答案,该答案低于投票结果
 ngOnDestroy() {
        this.buy_socket.unsubscribe();
        console.log("order-buy ngDestroy called!");
    }