【发布时间】:2019-09-14 00:19:18
【问题描述】:
我有一个搜索栏,每个字母都会更新结果,但是当用户快速输入 3 个字母时,之前的请求不会被取消,所以在他得到结果之前会有一个难看的延迟。
我已阅读此https://github.com/axios/axios#cancellation,也许我有点累,但我很难将它添加到我的项目中。它几乎起到了相反的效果,现在它需要永远。 您有什么建议或者您推荐一个好的教程以便我理解这一点吗?
<input v-model="query" type="text" class="form-control" placeholder="Runner name or description"
aria-label="Runner name or description"
aria-describedby="basic-addon2">
watch: {
query: {
handler: _.debounce(function () {
this.newSearch()
}, 100)
}
},
methods: {
searchItems() {
let filters = {
q: this.query
};
const CancelToken = axios.CancelToken;
const source = CancelToken.source();
axios.post('/Items/find', filters, {
cancelToken: source.token
})
.catch(function(thrown) {
if (axios.isCancel(thrown)) {
console.log('Request canceled', thrown.message);
}
})
.then(
response => {
if(this.Items!=null){
response.data.forEach(element => {
this.Items.push(element);
});
}
else
this.Items=response.data;
}
);
},
newSearch(){
const CancelToken = axios.CancelToken;
const source = CancelToken.source();
source.cancel('Cancel previous request');
this.countItems();
this.searchItems();
},
showMore(){
this.startindex = this.startindex+this.nbrows;
this.searchItems();
},
countItems(){
this.countItems=10;
let filters = {
q: this.query
};
axios.post('/Items/count', filters).then(
response => {
this.countItems=response.data;
}
);
}
}
【问题讨论】:
-
你试过增加去抖吗?