1.贴上代码
- 输入事件的函数,防抖处理,防止请求过多次搜索
- 对搜索历史数据持久化本地存储
- 点击关键字跳转详情页
- 保存搜索关键词
<template>
<view>
<!-- 搜索框 -->
<view class="search-box">
<uni-search-bar @confirm="search" @input="input" :radius="100" :focus="true" cancelButton="auto"
placeholder="搜索"></uni-search-bar>
</view>
<!-- 搜索建议列表 -->
<view class="sugg-list" v-if="searchResults.length!==0">
<view class="sugg-item" v-for="(item,i) in searchResults" :key="i" @click="gotoDetail(item)">
<view class="goods-name">
{{item.name}}
</view>
<uni-icons type="arrowright" size="16"></uni-icons>
</view>
</view>
<!-- 搜索历史 -->
<view class="history-box" v-else>
<!-- 搜索标题 -->
<view class="history-title">
<text>搜索历史</text>
<uni-icons type="trash" size="16" @click="clean"></uni-icons>
</view>
<!-- 搜索列表 -->
<view class="history-list">
<uni-tag :inverted="true" type="primary" :text="item" v-for="(item,i) in histores" :key="i" @click="gotoGoodsList(item)"></uni-tag>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
timer: null,
//搜索的关键字
kw: '',
//搜索的结果列表
searchResults: [],
//搜索历史的数据
historyList: ['a', 'b132154']
};
},
onLoad() {
//获取本地存储的搜索记录
this.historyList=JSON.parse(uni.getStorageSync('kw')||'[]')
},
methods: {
//输入事件的函数,防抖处理,防止请求过多次搜索
input(e) {
//清除timer
clearTimeout(this.timer)
//500毫秒没有触发输入事件,则为搜索关键字赋值
this.timer = setTimeout(() => {
this.kw = e;
this.getSearchList()
}, 500)
},
//获取搜索的内容列表
getSearchList() {
//判断搜索关键字是否为空
if (this.kw.length === 0) {
this.searchResults = []
return;
}
uni.request({
url: uni.$baseUrl + '/api/Home/GetSearchList',
data: {
query: this.kw
}
}).then((res) => {
// console.log(res.data.data);
if (res.data.code != 200) return uni.$showMsg();
this.searchResults = res.data.data;
});
// 保存搜索关键词
this.saveSearchHistory()
},
//点击关键字跳转详情页
gotoDetail(item) {
uni.navigateTo({
url: '/subpkg/productDetail/productDetail?id=' + item.id
});
},
// 保存搜索关键词
saveSearchHistory(){
//this.historyList.push(this.kw)
//解决关键词重复问题
const set=new Set(this.historyList);
//移除原对应元素
set.delete(this.kw);
//添加新元素
set.add(this.kw);
this.historyList=Array.from(set);
//对搜索历史数据持久化本地存储
uni.setStorageSync('kw',JSON.stringify(this.historyList))
},
//清空搜索记录
clean(){
this.historyList=[];
uni.setStorageSync('kw','[]')
},
//跳转到商品列表页
gotoGoodsList(kw){
uni.navigateTo({
url:'/subpkg/goods_list/goods_list?query='+kw
})
}
},
computed: {
histores(){
//由于数组是引用类型,所以不要直接基于原数组调用reverse方法,以免修改原数组中的顺序
//新建一个内存无关的数组,再进行reverse反转
return [...this.historyList].reverse()
}
}
}
</script>
<style lang="scss">
.search-box {
background-color: #55aaff;
position: sticky;
z-index: 999;
}
.sugg-list {
padding: 0 5px;
.sugg-item {
font-size: 12px;
padding: 13px 0;
border-bottom: 1px solid #efefef;
display: flex;
align-items: center;
justify-content: space-between;
.goods-name {
// 文字不允许换行《单行文本)
white-space: nowrap;
// 溢出部分隐藏
overflow: hidden;
// 文本溢出后,使用...代智
text-overflow: ellipsis;
margin-right: 3px;
}
}
}
.history-box {
padding: 0 5px;
.history-title {
display: flex;
justify-content: space-between;
font-size: 13px;
align-items: center;
height: 40px;
border-bottom: 1px solid #efefef;
}
.history-list {
display: flex;
flex-wrap: wrap;
.uni-tag{
margin-top: 5px;
margin-right: 5px;
}
}
}
</style>
2.效果示例