微信小程序 本地缓存wx.setStorage缓存设置和wx.removeStorage缓存删除

Page({

  /**
   * 页面的初始数据
   */
  data: {
    storageData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    console.log("getStorageSync  " + Array.isArray(wx.getStorageSync('testKey')));
    if (!Array.isArray(wx.getStorageSync('testKey'))) //判断本地缓存是否数组
    {
      wx.setStorage({
        key: 'testKey',
        data: that.data.storageData
      })
    }
  },

  // 删除本地缓存
  delBtnClick: function() {
    var that = this;
    wx.removeStorage({
      key: 'testKey',
      success: function(res) {
        that.setData({
          storageData: []
        })
      },
    })
  },

  // 添加本地缓存
  addBtnClick: function() {
    var that = this;
    if (!Array.isArray(wx.getStorageSync('testKey'))) //判断本地缓存是否数组
    {
      wx.setStorage({
        key: 'testKey',
        data: that.data.storageData
      })
    } else {
      var logs = wx.getStorageSync('testKey')
      logs.unshift(Date.now())
      wx.setStorageSync('testKey', logs)
    }
  }
})

 

你可能感兴趣的:(微信小程序)