wx.setStorageSync(KEY,DATA)


wx.setStorageSync

每個微信小程序都可以有自己的本地緩存,可以通過wx.setStorage(wx.setStorageSync)、wx.getStorage(wx.getStorageSync)、wx.clearStorage(wx.clearStorageSync)可以對本地緩存進行設置、獲取和清理。
每個微信小程序都可以有自己的本地緩存,可以通過 wx.setStorage(wx.setStorageSync)、wx.getStorage(wx.getStorageSync)、wx.clearStorage(wx.clearStorageSync)可以對本地緩存進行設置、獲取和清理。
注意: localStorage 是永久存儲的,但是我們不建議將關鍵信息全部存在 localStorage,以防用戶換設備的情況。

wx.setStorage(OBJECT)

將數據存儲在本地緩存中指定的 key 中,會覆蓋掉原來該 key 對應的內容,這是一個異步接口。
OBJECT參數說明:
參數 類型 必填 說明
key String 本地緩存中的指定的 key
data Object/String 需要存儲的內容
success Function 接口調用成功的回調函數
fail Function 接口調用失敗的回調函數
complete Function 接口調用結束的回調函數(調用成功、失敗都會執行)
示例代碼
wx.setStorage({  key:"key"  data:"value"})

wx.setStorageSync(KEY,DATA)

將 data 存儲在本地緩存中指定的 key 中,會覆蓋掉原來該 key 對應的內容,這是一個同步接口。
參數說明:
參數 類型 必填 說明
key String 本地緩存中的指定的 key
data Object/String 需要存儲的內容
示例代碼
wx.setStorageSync('key', 'value')

wx.getStorage(OBJECT)

從本地緩存中異步獲取指定 key 對應的內容。
OBJECT參數說明:
參數 類型 必填 說明
key String 本地緩存中的指定的 key
success Function 接口調用的回調函數,res = {data: key對應的內容}
fail Function 接口調用失敗的回調函數
complete Function 接口調用結束的回調函數(調用成功、失敗都會執行)
示例代碼:
wx.getStorage({  key: 'key',  success: function(res) {      console.log(res.data)  } })

wx.getStorageSync(KEY)

從本地緩存中同步獲取指定 key 對應的內容。
參數說明:
參數 類型 必填 說明
key String 本地緩存中的指定的 key
示例代碼:
var value = wx.getStorageSync('key')

wx.clearStorage()

清理本地數據緩存。
示例代碼:
wx.clearStorage()

wx.clearStorageSync()

同步清理本地數據緩存
示例代碼:
wx.clearStorageSync()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM