常用搜索中搜索历史,搜索推荐功能
此篇文章只供自己参考 我只是代码的搬运工
本人使用uni写的H5页面(移动端),及供参考
HTML:
<template>
<view class="page">
<view class="header">
<view class="inputBox">
<input v-model="inpuValue" type="text" @confirm="search" />
<image src="https://shanxi-petrochina.oss-cn-hangzhou.aliyuncs.com/home/ico_sousuo%402x.png"></image>
</view>
<text class="headerText" @click="search">搜索</text>
</view>
<view class="historyBox" v-show="historyShow">
<view class="historyTop">
<text>历史搜索</text>
<image src="https://shanxi-petrochina.oss-cn-hangzhou.aliyuncs.com/home/ico_shanchu.png" @click="Delete"></image>
</view>
<view class="historyItem">
<text v-for="(item,index) in tagValueList" :key="index" @click="tagClick(item)">{{item}}</text>
</view>
</view>
<view class="historyBox">
<view class="historyTop">
<text>猜你可能在找</text>
</view>
<view class="historyItem">
<text v-for="(item,index) in historyItemList" :key="index" @click="tagClick(item.name)">{{item.name}}</text>
</view>
</view>
</view>
</template>
CSS:
<style lang="scss" scoped>
.page{
min-height: 100vh;
background: #F5F5F5;
padding: 20rpx 24rpx;
}
.header{
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
.inputBox{
width: 606rpx;
height: 60rpx;
background: #ECECEC;
border-radius: 30rpx;
position: relative;
input{
height: 60rpx;
background: #ECECEC;
border-radius: 30rpx;
padding-left: 72rpx;
padding-right: 20rpx;
font-size: 28rpx;
color: #000000;
}
image{
width: 32rpx;
height: 32rpx;
position: absolute;
left: 20rpx;
top: 0;
bottom: 0;
margin: auto;
}
}
.headerText{
font-size: 32rpx;
color: #606060;
line-height: 44rpx;
}
}
.historyBox{
width: 100%;
margin-top: 40rpx;
.historyTop{
display: flex;
align-items: center;
justify-content: space-between;
text{
font-size: 28rpx;
font-weight: 600;
color: #333333;
line-height: 40rpx;
}
image{
width: 29rpx;
height: 28rpx;
}
}
.historyItem{
display: flex;
align-items: center;
flex-wrap: wrap;
margin-top: 20rpx;
text{
display: inline-block;
height: 44rpx;
background: #ECECEC;
border-radius: 30rpx;
margin-bottom: 10rpx;
margin-right: 20rpx;
padding: 4rpx 10rpx 6rpx 10rpx;
font-size: 24rpx;
color: #606060;
line-height: 44rpx;
}
}
}
</style>
JS:
<script>
export default {
data() {
return {
inpuValue : '',
tagValueList : [],
historyShow : true,
historyItemList : [
{
name : '美团外卖'
},
{
name : '饿了么'
},
{
name : '爱奇艺'
},
]
}
},
onShow() {
try {
const value = uni.getStorageSync('tagValue');
if (value.length>0) {
this.tagValueList = value
this.historyShow = true
}else{
this.historyShow = false
}
} catch (e) {
// error
this.historyShow = false
}
},
methods: {
// 点击搜索
search(){
if(this.inpuValue==''){
uni.showToast({
title:'请输入您要搜索的内容',
icon:'none'
})
}else{
let tagValue = uni.getStorageSync("tagValue");
tagValue = tagValue ? tagValue :[];
tagValue.unshift(this.inpuValue);
if(tagValue.length > 10){
tagValue.splice(10,1)
}
tagValue = Array.from(new Set(tagValue));
uni.setStorage({
key:"tagValue",
data:tagValue
})
uni.navigateTo({
url:'sousuoResult/sousuoResult?value='+ this.inpuValue
})
}
},
// 点击删除
Delete(){
this.historyShow = false
uni.setStorage({
key:"tagValue",
data:[]
})
},
// 点击标签
tagClick(item){
let tagValue = uni.getStorageSync("tagValue");
tagValue = tagValue ? tagValue :[];
tagValue.unshift(item);
if(tagValue.length > 10){
tagValue.splice(10,1)
}
tagValue = Array.from(new Set(tagValue));
uni.setStorage({
key:"tagValue",
data:tagValue
})
this.inpuValue = item
uni.navigateTo({
url:'sousuoResult/sousuoResult?value='+ item
})
}
}
}
</script>