取消/杀死http api调用角度2

Jee*_*mar 5 api http angular2-services angular2-http angular

有时httpapi调用需要很长时间才能加载数据.在这种情况下,如果我们移动另一个组件,它仍然继续执行(我们可以在浏览器控制台中看到它).那么,http当我们移动另一个组件时,有什么方法可以取消或杀死api调用?

Ste*_*ota 4

您可以通过使用生命周期事件unsubscribe()中的方法“杀死”它OnDestroy,假设您正在使用订阅,例如:

mySubscription: any;

ngOnInit() {
    this.mySubscription = this.myHttpCall().subscribe...
}

ngOnDestroy() {
    this.mySubscription.unsubscribe();
}
Run Code Online (Sandbox Code Playgroud)