搜索组件:
<view class="search-bar">
<uni-search-bar placeholder="" class="search-bar-content" @confirm="search" v-model="searchVal"
@input="input" :focus="true" bgColor="#EEEEEE" radius="50" cancelButton="always">
<template v-slot:searchIcon>
<uni-icons class="search-bar-icon" custom-prefix="bjsicons" type="bjs-search" size="30"
@click="search"></uni-icons>
</template>
</uni-search-bar>
</view>
外部防抖函数js:
export const debounce = (func, wait) => {
let timerId = null;
console.log(timerId);
return function(){
let _This = this,
args = arguments;
if(timerId) clearTimeout(timerId);
timerId = setTimeout(function(){
func.apply(_This,args);
},wait);
}
}
function throttle(){
console.log("throttle");
}
module.exports = {
throttle,
debounce
}
调用防抖函数和搜索方法:注意这里debounce方法的写法。之前写在input函数体内,对searchAdvise进行防抖无效
search() {
// 只有点击搜索才设置进搜索历史
this.updateHistory(this.searchVal);
},
input: debounce(function (e) {
this.searchVal = e;
this.searchAdvise(this.searchVal);
}, 1000),
async searchAdvise(keyword){
const { list } = await unifyRequest(getSearchAdvise,keyword);
this.searchList = list;
console.log(list,this.searchList);
},