1. 程式人生 > >微信小程式資料快取Storage

微信小程式資料快取Storage

1.存放資料

wx.setStorage(Object object)

將資料儲存在本地快取中指定的 key 中。會覆蓋掉原來該 key 對應的內容。資料儲存生命週期跟小程式本身一致,即除使用者主動刪除或超過一定時間被自動清理,否則資料都一直可用。資料儲存上限為 10MB。

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

wx.setStorageSync(string key, Object|string data)

wx.setStorage 的同步版本

引數:  string key:本地快取中指定的 key

          Object|string data需要儲存的內容

wx.setStorageSync('key', 'value')

2.獲取資料

wx.getStorage(Object object)

從本地快取中非同步獲取指定 key 的內容

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

wx.getStorageSync(string key)

wx.getStorage 的同步版本

引數:  string key:本地快取中指定的 key

返回值:  Object|string data:key對應的內容

 var value = wx.getStorageSync('key')

3.清除快取

wx.clearStorage(Object object)

清理本地資料快取

wx.clearStorage()

4.清除指定快取

wx.removeStorage(Object object)

從本地快取中移除指定 key

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