1.实现效果
2.实现原理
将数据存在storage中。
wx.setStorageSync('search_history', JSON.stringify(this.data.list))
取数组前15条。
if (wx.getStorageSync('search_history') ){
this.setData({
list:JSON.parse(wx.getStorageSync('search_history') ).slice(0, 15)
})
}
并将每次选择的数据移到数组的第一条。
this.data.list.unshift(data);
清空所有历史。
clearHistory() {
this.setData({
list:[]
})
wx.removeStorageSync('search_history')
},
3.微信小程序中将数据存在storage中,除非用户手动触发,否则不会过期,同理,若想在浏览器中实现,只需将数据存在localStorage中即可。
4.代码
<view class=