微信小程式之快取——不同頁面傳遞資料
阿新 • • 發佈:2018-11-23
1. 新增快取
單個金鑰允許儲存的最大資料長度為1MB,所有資料儲存上限為10MB。
// 儲存資訊到storage // 非同步儲存 set() { wx.setStorage({ key: 'user', data: 'cck', success: ()=> { console.log('儲存成功'); } }) }, // 同步儲存 set() { try { wx.setStorageSync('user', 'cck') } catch (e) { } }
2. 獲取快取
從本地快取中非同步獲取指定key的內容
// 非同步
wx.getStorage({
key: 'user',
success (res) {
console.log(res.data)
}
})
// 同步
try {
var value = wx.getStorageSync('user')
if (value) {
// Do something with return value
}
} catch (e) {
// Do something when catch error
}
3. 移除快取
從本地快取中移除指定 key
// 非同步 wx.removeStorage({ key: 'user', success (res) { console.log(res.data) } }) // 同步 try { wx.removeStorageSync('user') } catch (e) { // Do something when catch error }