【问题标题】:cancel all pending requests angular4取消所有待处理的请求 angular4
【发布时间】:2018-07-12 13:48:44
【问题描述】:

如何取消 angular2 中的所有未决承诺。我知道承诺是不可取消的,但是有什么解决方法吗?如何在注销时取消所有待处理的请求。

api服务

get(url: string) {
        let that = this;
        let getPromise = new Promise((resolve, reject) => {
            this
                .httpService
                .get(url)
                // ...and calling .json() on the response to return data
                .map((res: Response) => res.json())
                .subscribe(response => {
                    resolve(response);
                }, (error) => {
                    console.log(error);
                    reject(that.handleError(error));
                }, () => {
                    console.log('API Subscription Complete!');
                });
        });
        return getPromise;
    }

组件服务

fetchData(url) {
        return new Promise(resolve => {
            this
                .apiService
                .get(url)
                .then(response => {
                    resolve(response);
                }).catch(error=>{
                    console.log(error);
                });
        });
    }

【问题讨论】:

标签: angular2-services angular2-http


【解决方案1】:

你可以使用 unsubscribe(); 的 observable

subscribeRequest.unsubscribe();

更多信息请参考This article

【讨论】:

猜你喜欢
  • 2014-10-07
  • 2020-11-05
  • 1970-01-01
  • 2021-01-05
  • 2015-11-25
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
  • 2016-07-11
相关资源
最近更新 更多