微信小程序默认创建的应用,点击用户头像,会显示log日志,日志存储在Storage中,key值是logs,value值是Array数组,数组的长度为8
onLaunch() { // 展示本地存储能力 const logs = wx.getStorageSync('logs') || [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) }
说明:
onLoad() { this.setData({ logs: (wx.getStorageSync('logs') || []).map(log => { return { date: util.formatTime(new Date(log)), timeStamp: log } }) }) }
<block wx:for="{{logs}}" wx:key="timeStamp" wx:for-item="log"> <text class="log-item">{{index + 1}}. {{log.date}}</text> </block>
界面
data: { jsonData: { name: '张三',age: 18} }, addBasic() { /**原生类型 */ wx.setStorage({ key: 'key-basic', data: '张三' }) }, addDate() { /**Date类型 */ wx.setStorage({ key: 'key-date', data: util.formatTime(new Date()) }) }, addJson() { /**JSON.stringify序列化的对象 */ wx.setStorage({ key: 'key-json', data: this.data.jsonData, success: function () { console.log("Storage--success"); } }) },
removeBasic(){ wx.removeStorage({ key: 'key-basic', }) },
storageInfo(){ wx.getStorageInfo({ success: (option) => { console.log(option); }, }) },
clearStorageInfo(){ wx.clearStorage({ success: (res) => { console.log("数据清除成功"); }, }) }