Aurelia HttpClient取消请求

时间:2018-09-21 14:50:43

标签: aurelia aurelia-http-client

我正在尝试构建一个自动完成组件,并希望在输入时取消对服务器的未解决请求。

在HttpClient的文档中找不到与此相关的文档。它提到它是可取消的(与fetch不同),但不是。 https://aurelia.io/docs/plugins/http-services

目前,我已经盲目地将它们拼凑在一起,不足为奇的是,它甚至没有中断请求:

async searchTermChanged(newValue, oldValue) {   
    if (newValue.length < 3)
      return;

    if (this.promises.length) {
       this.promises.forEach(x => x.abort());
       //should probably remove too
    }

    var promise = this.httpClient.post(this.endpoint, { SearchTerm: newValue });
    this.promises.push(promise);

    var data = await promise;

    var response = JSON.parse(data.response);

    this.results = response;
  }
}

在哪里可以找到有关如何提出可取消请求的更多信息?我的Google-fu使我失败了。

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

this.client["pendingRequests"].forEach(request => {
   request.abort();
});

由于我正在使用TypeScript,因此我不得不做["pendingRequests"],而且数组似乎不在定义之内。

注意:我还在每个自动完成操作中使用了范围限定的HttpClient,因此当它取消所有先前的请求时,不会意外取消该应用程序正在请求的其他内容。

相关问题