我们在开发的过程中,常常会用到本地存储,下面我给大家分享一下微信本地存储的使用。
需要存储的内容。只支持原生类型、Date、及能够通过JSON.stringify序列化的对象。
1 try { 2 wx.setStorageSync(‘key‘, ‘value‘) 3 } catch (e) { 4 5 }
1 try { 2 wx.removeStorageSync(‘key‘) 3 } catch (e) { 4 // Do something when catch error 5 }
1 try { 2 var value = wx.getStorageSync(‘key‘) 3 if (value) { 4 // Do something with return value 5 } 6 } catch (e) { 7 // Do something when catch error 8 }
1 try { 2 const res = wx.getStorageInfoSync() 3 console.log(res.keys) 4 console.log(res.currentSize) 5 console.log(res.limitSize) 6 } catch (e) { 7 // Do something when catch error 8 }
1 try { 2 wx.clearStorageSync() 3 } catch(e) { 4 // Do something when catch error 5 }
将数据存储在本地缓存中指定的 key 中。会覆盖掉原来该 key 对应的内容。数据存储生命周期跟小程序本身一致,即除用户主动删除或超过一定时间被自动清理,否则数据都一直可用。单个 key 允许存储的最大数据长度为 1MB,所有数据存储上限为 10MB。
1 wx.setStorage({ 2 key:"key", 3 data:"value"})
1 wx.removeStorage({ 2 key: ‘key‘, 3 success (res) { 4 console.log(res) 5 }})
1 wx.getStorage({ 2 key: ‘key‘, 3 success (res) { 4 console.log(res.data) 5 }})
1 wx.getStorageInfo({ 2 success (res) { 3 console.log(res.keys) 4 console.log(res.currentSize) 5 console.log(res.limitSize) 6 }})
1 wx.clearStorage()
以上就是微信给我们提供的本地存储的方法,希望对大家有帮助!
原文:https://www.cnblogs.com/le220/p/10963911.html