<el-autocomplete
v-model="keyword"
:fetch-suggestions="querySearchAsync"
placeholder="请输入内容"
@select="handleSelect"
@input="search"
:trigger-on-focus="false"
>
<el-button @click="gotoSearch"></el-button>
</el-autocomplete>
methods: {
//选择联想关键词时触发
async handleSelect(val) {
let vm = this;
await vm.getSearchData();
vm.showSearchList = true;
vm.$nextTick(() => {
vm.$refs.SearchComponents.getSearchType(
JSON.stringify(val.type)
);
});
},
//点击搜索时触发
search() {
this.getSearchData();
let vm = this;
if (vm.keyWord.length == 0) {
vm.showSearchList = false;
}
},
//获取接口返回的联想关键词列表
async querySearchAsync(queryString, cb) {
let vm = this;
if (vm.keyWord != "") {
let queryList = await vm.getSearchData();
queryList = queryList.map((item) => {
return { value: item.title, type: item.type };
});
cb(queryList);
}
},
getSearchData() {
let vm = this;
return new Promise((resolve, reject) => {
vm.$req
.doRequest({
alias: "dcs-search-globalSearch",
query: {
keyword: vm.keyWord,
pageNum: 1,
pageSize: 10,
type: "0",
},
})
.then((res) => {
if (res.code == 0) {
vm.keywordList = res.data;
vm.searchTyp =
vm.keywordList.length != 0
? JSON.stringify(vm.keywordList[0].type)
: "2";
}
return resolve(res.data);
});
});
},
}
【注意】querySearchAsync的返回结果列表的数组的对象里要有key值名为value的值,
this.keyWordList = this.keyWordList = this.keyWordList.map((item) => {
return { value: item.title }; //结果列表里一定要有value值
});