api - 取消/杀死 http api 调用 Angular 2

标签 api http angular angular2-services angular2-http

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

最佳答案

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

mySubscription: any;

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

ngOnDestroy() {
    this.mySubscription.unsubscribe();
}

关于api - 取消/杀死 http api 调用 Angular 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41719594/

相关文章:

ios - 提取被重定向到自定义 url 方案并失败

Python OAuth WooCommerce

c# - 使用 HTTP PUT 将文件上传到 Sharepoint (WSS 3.0) 文档库

api - 带有分支名称的 jenkins 的 bitbucket webhook

c - 将 CURL 结果放入字符串而不是 STDOUT?

angular - 在多命名路由器 socket 的情况下获取当前 socket 名称

json - 在 rxjs ajax 回调中解析 JSON 不起作用

angular - 在 Sequelize 中返回对象而不是一个对象的数组

javascript - 在 JavaScript 中实现 Google map API key

ios - 如何在 UI TextView 中显示 YouTube 搜索 API 响应?