微信小程序--数据缓存

小程序数据缓存

wx.setStorageSync
wx.setStorage
wx.getStorage
wx.getStorageSync
wx.clearStorageSync
wx.clearStorage

异步缓存
wx.setStorage
将数据存储在本地缓存中指定的 key 中

wx.setStorage({
  key:"key",
  data:"value"
})

wx.getStorage
从本地缓存中异步获取指定 key 的内容

wx.getStorage({
  key: 'key',
  success (res) {
    console.log(res.data)
  }
})

wx.removeStorage
从本地缓存中移除指定 key

wx.removeStorage({
  key: 'key',
  success (res) {
    console.log(res)
  }
})

wx.getStorageInfo
异步获取当前storage的相关信息

wx.getStorageInfo({
  success (res) {
    console.log(res.keys)
    console.log(res.currentSize)
    console.log(res.limitSize)
  }
})

同步缓存
wx.setStorageSync(string key, any data)
本地缓存中指定的 key
需要存储的内容。只支持原生类型、Date、及能够通过JSON.stringify序列化的对象
wx.removeStorageSync(string key)
本地缓存中指定的 key
wx.getStorageInfoSync
当前 storage 中所有的 key

清空缓存
wx.clearStorage
清理本地数据缓存
wx.clearStorage()

使用同步存token
在这里插入图片描述
在这里插入图片描述
取token

let token=wx.getStorageSync('token')

版权声明:本文为dgq2739710006原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。