我正在尝试构建一个自动完成组件,并希望使其在键入时取消对服务器的 Unresolved 请求。
我在 HttpClient 的文档中找不到与此相关的文档。它提到它是可以取消的(与获取不同),但没有提到如何取消。 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 失败了。
最佳答案
看起来你可以这样做:
this.client["pendingRequests"].forEach(request => {
request.abort();
});
我必须执行["pendingRequests"]
,因为我使用的是 TypeScript,并且该数组似乎不在定义范围内。
注意:我还在每个自动完成中使用了范围内的 HttpClient,这样当它取消所有先前的请求时,它就不会意外取消应用程序正在请求的其他内容。
关于Aurelia HttpClient 取消请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52446271/