取消/杀死 http api call angular 2
Cancel /Kill http api call angular 2
有时http
api调用需要很长时间来加载数据。在这种情况下,如果我们移动到另一个组件,它仍然会继续执行(我们可以在浏览器控制台中看到它)。那么,有什么方法可以在我们移动到另一个组件时取消或终止 http
api 调用?
您可以 "kill" 通过在 OnDestroy
生命周期事件中使用 unsubscribe()
方法,假设您正在使用订阅,例如:
mySubscription: any;
ngOnInit() {
this.mySubscription = this.myHttpCall().subscribe...
}
ngOnDestroy() {
this.mySubscription.unsubscribe();
}
您可以使用 rxjs 中的 takeWhile()
函数取消订阅所有订阅:示例:
http://brianflove.com/2016/12/11/anguar-2-unsubscribe-observables/
有时http
api调用需要很长时间来加载数据。在这种情况下,如果我们移动到另一个组件,它仍然会继续执行(我们可以在浏览器控制台中看到它)。那么,有什么方法可以在我们移动到另一个组件时取消或终止 http
api 调用?
您可以 "kill" 通过在 OnDestroy
生命周期事件中使用 unsubscribe()
方法,假设您正在使用订阅,例如:
mySubscription: any;
ngOnInit() {
this.mySubscription = this.myHttpCall().subscribe...
}
ngOnDestroy() {
this.mySubscription.unsubscribe();
}
您可以使用 rxjs 中的 takeWhile()
函数取消订阅所有订阅:示例:
http://brianflove.com/2016/12/11/anguar-2-unsubscribe-observables/