您好,登錄后才能下訂單哦!
這篇文章主要講解了“微信小程序中數(shù)據(jù)緩存的方法”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“微信小程序中數(shù)據(jù)緩存的方法”吧!
每個(gè)微信小程序都可以有自己的本地緩存,可以通過(guò) wx.setStorage(wx.setStorageSync)、wx.getStorage(wx.getStorageSync)、wx.clearStorage(wx.clearStorageSync)可以對(duì)本地緩存進(jìn)行設(shè)置、獲取和清理。本地緩存最大為10MB。
注意: localStorage 是永久存儲(chǔ)的,但是我們不建議將關(guān)鍵信息全部存在 localStorage,以防用戶換設(shè)備的情況。
wx.setStorage(OBJECT)
將數(shù)據(jù)存儲(chǔ)在本地緩存中指定的 key 中,會(huì)覆蓋掉原來(lái)該 key 對(duì)應(yīng)的內(nèi)容,這是一個(gè)異步接口。
OBJECT參數(shù)說(shuō)明:
示例代碼
wx.setStorage({ key:"key" data:"value" })
wx.setStorageSync(KEY,DATA)
將 data 存儲(chǔ)在本地緩存中指定的 key 中,會(huì)覆蓋掉原來(lái)該 key 對(duì)應(yīng)的內(nèi)容,這是一個(gè)同步接口。
OBJECT參數(shù)說(shuō)明:
示例代碼
try { wx.setStorageSync('key', 'value') } catch (e) { }
wx.getStorage(OBJECT)
從本地緩存中異步獲取指定 key 對(duì)應(yīng)的內(nèi)容。
OBJECT參數(shù)說(shuō)明:
示例代碼:
wx.getStorage({ key: 'key', success: function(res) { console.log(res.data) } })
wx.getStorageSync(KEY)
從本地緩存中同步獲取指定 key 對(duì)應(yīng)的內(nèi)容。
參數(shù)說(shuō)明:
示例代碼:
try { var value = wx.getStorageSync('key') if (value) { // Do something with return value } } catch (e) { // Do something when catch error }
wx.getStorageInfo(OBJECT)
異步獲取當(dāng)前storage的相關(guān)信息
OBJECT參數(shù)說(shuō)明:
success返回參數(shù)說(shuō)明:
示例代碼:
wx.getStorageInfo({ success: function(res) { console.log(res.keys) console.log(res.currentSize) console.log(res.limitSize) } })
wx.getStorageInfoSync
同步獲取當(dāng)前storage的相關(guān)信息
示例代碼:
try { var res = wx.getStorageInfoSync() console.log(res.keys) console.log(res.currentSize) console.log(res.limitSize) } catch (e) { // Do something when catch error }
wx.removeStorage(OBJECT)
從本地緩存中異步移除指定 key 。
OBJECT參數(shù)說(shuō)明:
示例代碼:
wx.removeStorage({ key: 'key', success: function(res) { console.log(res.data) } })
wx.removeStorageSync(KEY)
從本地緩存中同步移除指定 key 。
參數(shù)說(shuō)明:
示例代碼:
try { wx.removeStorageSync('key') } catch (e) { // Do something when catch error }
wx.clearStorage()
清理本地?cái)?shù)據(jù)緩存。
示例代碼:
wx.clearStorage()
wx.clearStorageSync()
同步清理本地?cái)?shù)據(jù)緩存
示例代碼:
try { wx.clearStorageSync() } catch(e) { // Do something when catch error }
感謝各位的閱讀,以上就是“微信小程序中數(shù)據(jù)緩存的方法”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)微信小程序中數(shù)據(jù)緩存的方法這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。