uni-app搜索页面(本地历史记录缓存)

文章发布时间:

最后更新时间:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<template>
<view class="container">
<view class="search-btn" @click="focus()">
<uni-search-bar @confirm="search" bgColor="#EDEDED" radius="20" cancelButton="none" placeholder="输入模板关键字"
v-model="searchValue"> </uni-search-bar>
</view>
<view class="history">
<view class="title">
<text style="font-weight: 900;">历史记录</text>
<uni-icons type="trash" size="20" @click="clean()"></uni-icons>
</view>
<view class="example-body">
<view class="tag-view" v-for="(item,index) in list" @longpress="deleted(index)">
<uni-tag :circle="true" :text="item" @click="tohistory(item)"
custom-style="background-color: #F4F4F4; border-color:#F4F4F4; color: #2f2f2f;" />
</view>
</view>
</view>
<uni-popup ref="popup" type="dialog">
<uni-popup-dialog mode="base" message="成功消息" title="确认删除?" :duration="2000" :before-close="true"
@close="close" @confirm="confirm"></uni-popup-dialog>
</uni-popup>
<uni-popup ref="clean" type="dialog">
<uni-popup-dialog mode="base" message="成功消息" title="删除全部历史记录?" :duration="2000" :before-close="true"
@close="close" @confirm="clearall"></uni-popup-dialog>
</uni-popup>
</view>
</template>

<script>
export default {
data() {
return {
searchValue: '',
list: [],
index: ''
}
},
methods: {
// 去重
removeDuplicate(arr) {
const newArr = []
arr.forEach(item => {
if (newArr.indexOf(item) === -1) {
newArr.push(item)
}
})
return newArr // 返回一个新数组
},
search(res) {

this.list.push(res.value)
this.list = this.removeDuplicate(this.list)
uni.setStorageSync('history', this.list)

uni.navigateTo({
url:'../calorie_category/calorie_category?word='+ res.value
})
this.searchValue = ''
},
deleted(index) {
this.$refs.popup.open()
this.index = index
},
clean() {
this.$refs.clean.open()
},
clearall() {
this.list = []
uni.setStorageSync('history', this.list)
this.$refs.clean.close()
},
confirm() {
this.list.splice(this.index, 1)
this.$refs.popup.close()
this.index = ''
uni.setStorageSync('history', this.list)
},
close() {
this.$refs.popup.close()
this.$refs.clean.close()
},
tohistory(item) {
uni.navigateTo({
url:'../calorie_category/calorie_category?word='+ item
})
},
focus() {

}
},
onLoad() {
if (uni.getStorageSync('history')) {
this.list = uni.getStorageSync('history')
}
}
}
</script>

<style>
.container {
height: 100%;
background-color: white;
}

.history {
margin: 20rpx;
}

.title {
display: flex;
justify-content: space-between;
margin: 20rpx 0;
}

.example-body {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: flex-end;
flex-wrap: wrap;
}

.tag-view {
margin-right: 10px;
}

</style>