gpt4 book ai didi

vuejs2 - Vue.js - Element UI - 输入远程搜索去抖动

转载 作者:行者123 更新时间:2023-12-05 08:42:08 25 4
gpt4 key购买 nike

我正在使用 vue.js 2.3element-ui input remote search 组件。 documentation here

我希望能够使用 lodashdebounce 远程搜索,但我发现它很棘手,因为 中的 cb >querySearchAsync(queryString, cb)

https://jsfiddle.net/1tgqn7o8/

<div id="app">
Fetch suggestion counter : {{counter}}
<el-autocomplete v-model="state4" :fetch-suggestions="querySearchAsync" placeholder="Please input" @select="handleSelect"></el-autocomplete>
</div>

var Main = {
data() {
return {
links: [],
state4: '',
timeout: null,
counter: 0,
};
},
methods: {
loadAll() {
return [
{ "value": "vue", "link": "https://github.com/vuejs/vue" },
{ "value": "element", "link": "https://github.com/ElemeFE/element" },
{ "value": "cooking", "link": "https://github.com/ElemeFE/cooking" },
{ "value": "mint-ui", "link": "https://github.com/ElemeFE/mint-ui" },
{ "value": "vuex", "link": "https://github.com/vuejs/vuex" },
{ "value": "vue-router", "link": "https://github.com/vuejs/vue-router" },
{ "value": "babel", "link": "https://github.com/babel/babel" }
];
},
querySearchAsync(queryString, cb) {
this.counter ++;
var links = this.links;
var results = queryString ? links.filter(this.createFilter(queryString)) : links;

clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
cb(results);
}, 3000 * Math.random());
},
createFilter(queryString) {
return (link) => {
return (link.value.indexOf(queryString.toLowerCase()) === 0);
};
},
handleSelect(item) {
console.log(item);
}
},
mounted() {
this.links = this.loadAll();
}
};
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')

最佳答案

您可以像这样简单地添加 debounce 属性:

<el-autocomplete :debounce="500" :fetch-suggestions="querySearchAsync" />

See official documentation

干杯!

关于vuejs2 - Vue.js - Element UI - 输入远程搜索去抖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44334177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com