methods: {
// ...
doSearch(searchStr) {
// 检查搜索字符串是否为空或者已经存在于历史记录中
if (!searchStr || this.historyList.includes(searchStr)) {
return; // 如果为空或者已存在,则不执行搜索
}
// 将新的搜索词添加到历史记录数组的开头
this.historyList.unshift(searchStr);
// 确保历史记录不超过一定数量,例如6个
this.historyList = this.historyList.slice(0, 6);
// 更新本地存储中的历史记录
uni.setStorageSync("history", JSON.stringify(this.historyList));
// 执行搜索操作,例如调用API或过滤数据
// 这里需要根据实际的搜索逻辑来编写代码
console.log('搜索内容:', searchStr);
// 清空搜索框,并聚焦到搜索框(如果需要)
this.searchKeyword = '';
// 假设有一个方法用于聚焦搜索框
// this.focusSearchInput();
},
// ...
}