本地存储(Storage)
![](https://img2020.cnblogs.com/blog/1701959/202004/1701959-20200424193756947-1118050227.jpg)
初始化本地存储:
onLoad: function (options) { let that = this wx.getStorage({ key: 'searchlist', success: function (res) { console.log(res) that.setData({ searchhistory:res.data }) }, fail(e){ let searcharry = [] wx.setStorage({ key: 'searchlist', data: searcharry }) } }) }
根据input点击:
1.修改本地存储
2.初始化存储
3.进行重复字检索和空字符排除
4.待优化:查询到初始化存储失败,重新进行初始化并进行检索、排空并进行存储
最下面是删除:
1.删除本地存储并进行重新初始化
inputvalue:function(e){ let that = this, value = e.detail.value wx.getStorage({ key: 'searchlist', success: function(res) { let listarrey = res.data, searchS = listarrey.indexOf(value) if (searchS == -1 && value != ""){ listarrey.push(value) wx.setStorage({ key: 'searchlist', data: listarrey }) wx.getStorage({ key: 'searchlist', success: function(res) { that.setData({ searchhistory: res.data }) } }) } },fail(e){ let searcharry = [] wx.setStorage({ key: 'searchlist', data: searcharry }) } }) }
deletehistory:function(){ let that = this wx.removeStorage({ key: 'searchlist', success(res){ let searcharry = [] wx.setStorage({ key: 'searchlist', data: searcharry }) that.setData({ searchhistory: searcharry }) } }) }