1、如何獲取緩存:wx.getStorageSync()
var that = this;
//緩存中的購物車信息和購物車商品數量,同步至當前頁面
if (wx.getStorageSync('cartinfo') != that.data.cartinfo) {
// 若緩存中購物車信息,和當前頁面不一致,同步
var cinfo = wx.getStorageSync('cartinfo');
var cinfosize = wx.getStorageSync('cartinfo').length;
that.setData({
cartinfo: cinfo,
cartnum: cinfosize
})
}2、如何設置、修改緩存:
wx.setStorageSync("cartinfo", that.data.cartinfo);3、如何刪除緩存:
3.1、刪除單個緩存,如果是數組的緩存,后期還可能會用的,建議不要直接刪除,把他數組數據清空即可:
wx.setStorageSync("cartinfo", []);3.2、wx.clearStorage接口函數,這個函數不需要參數,可以清理本地的所有緩存。
wx.clearStorage接口函數
3.3、wx.clearStorageSync接口函數,這個函數用于同步清理本地數據緩存,該接口同樣也沒有參數。
3.4、wx.removeStorage接口函數:這個接口函數用于從本地緩存中異步刪除指定key對應內容;wx.removeStorageSync接口函數:這個函數用于從本地緩存中同步刪除指定key對應的內容;如:
wx.removeStorage({
key: "cartinfo",
success(res) {
console.log("緩存已清理")
wx.switchTab({
url: '/pages/my/my'
})
}
})
wx.removeStorageSync("cartinfo");