小程序使用緩存


設置緩存

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

移除緩存

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

獲取緩存

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

清理緩存

wx.clearStorage()

封裝一個Storage

export default {
  set: (data, key = 'userData') => {
    return wx.setStorageSync(key, data);
  },
  get: (key = 'userData') => {
    return wx.getStorageSync(key);
  },
  remove: (key = 'userData') => {
    return wx.removeStorageSync(key);
  },
  clear: () => {
    return wx.clearStorageSync();
  },
  Set: (data, key = 'userData') => {
    return wx.setStorage({ key, data });
  },
  Get: (key = 'userData') => {
    return wx.getStorage({ key });
  },
  Remove: (key = 'userData') => {
    return wx.removeStorage(key);
  },
  Clear: () => {
    return wx.clearStorage();
  }
};

使用封裝

import Storage from "./common/auth/Storage";
const storage = Storage.get("userData");
if (!storage) {
	return that.login_register();
}


免責聲明!

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



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