-- 本地数据存储分为同步存储和异步存储
简单说,就是存储数据到手机(或者其他移动设备)本地
异步存储的方法
存储:wx.setStorage(object) 相同key会覆盖
//以键值对的形式存储 传进去的是个对象
wx.setStorage({
key: 'key',
data: '我是storeage异步存储的信息',
success: function(res) {
console.log(res)
}
})
读取:wx.getStorage(object)
wx.getStorage({
//获取数据的key
key: 'key',
success: function(res) {
console.log(res)
that.setData({
storageContent: res.data
})
},
//失败会调用
fail: function(res) {
console.log(res)
}
})
清除数据存储,wx.clearStorage()里面可以写回调函数 成功,失败,完成
wx.clearStorage({
success: function(res) {
that.setData({
storageContent: ''
})
}
})
同步存储的方法
存数据 相同key会覆盖
wx.setStorageSync(key,data)
wx.setStorageSync('key', '我是同步存储的数据')
读数据
wx.getStorageSync(key) 存储是指定的key
// var that = this;
var value = wx.getStorageSync('key')
this.setData({
storageSyncContent: value
})
清除数据
wx.clearStorageSync() 不可写回调方法
wx.clearStorageSync()
wx.getStorageSync