一.异步存储
(1)wx.setStorage(OBJECT)
将数据存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个异步接口。
wx.setStorage({
key:"key",
data:"value",
success:function(res){
console.log(res); //接口调用成功的回调函数
},
fail:function(res){
console.log(res); //接口调用失败的回调函数
},
complete:function(res){
console.log(res); //接口调用结束的回调函数(不论失败还是成功)
}
})
(2)wx.getStorage(OBJECT)
从本地缓存中异步获取指定 key 对应的内容。
wx.getStorage({
key:"key",
success:function(res){
console.log(res); //接口调用成功的回调函数
},
fail:function(res){
console.log(res); //接口调用失败的回调函数
},
complete:function(res){
console.log(res); //接口调用结束的回调函数(不论失败还是成功)
}
})
二. 同步存储
(1)wx.setStorageSync(KEY,DATA)
将 data 存储在本地缓存中指定的 key 中,会覆盖掉原来该 key 对应的内容,这是一个同步接口。
wx.setStorageSync('key', 'value')
(2)wx.getStorageSync(KEY)
从本地缓存中同步获取指定 key 对应的内容。
wx.getStorageSync('key')
三. 数据删除 (1)wx.removeStorage(OBJECT)
从本地缓存中异步移除指定 key 。
wx.removeStorage({
key: 'key',
success: function(res) {
console.log(res.data)
},
fail: function(res) {
console.log(res.data)
},
complete: function(res) {
console.log(res.data)
}
})
(2) wx.removeStorageSync(KEY)
从本地缓存中同步移除指定 key 。
wx.removeStorageSync('key')
(3) wx.clearStorage()
清理本地数据缓存。
wx.clearStorage()
(4) wx.clearStorageSync()
同步清理本地数据缓存
wx.clearStorageSync()