标签部分
<el-input style="width:200px;" placeholder="请输入" v-model="searchInput" clearable />
data部分略…
监听器部分
watch: {
'searchInput': {
deep: true,
handler(newVal, oldVal) {
this.debounce(this.getUpList, 500);
}
}
},
methods方法部分
debounce(fn, wait) {
if (this.timer !== null) {
clearTimeout(this.timer)
}
this.timer = setTimeout(fn, wait);
},
getUpList() {
console.log(this.searchInput);
// 请求...
},
本文介绍了如何使用 Vue.js 实现一个具有实时搜索功能的输入框,结合监听器和防抖函数(debounce)来优化性能,避免频繁触发请求。在数据部分定义了搜索输入框的 v-model,并在方法中定义了防抖函数和获取列表的方法,当搜索输入变化时,延迟500毫秒后调用获取列表的接口。
2249

被折叠的 条评论
为什么被折叠?



