uni调用本地存储 简单封装(个人笔记)

198 阅读1分钟
// 做缓存的工具  类具有更强的封装性
class LocalCache {
  setCache(key, value) { 
    uni.setStorageSync(key, JSON.stringify(value))
  }
  getCache(key) { //获取缓存
    const value = uni.getStorageSync(key)
    return value ? JSON.parse(value) : null
  }
  removeCache(key){ //清除指定缓存
	  uni.removeStorage({key})
  }
  clearCache() { //清除所有缓存
   uni.clearStorageSync()
  }
}
export default new LocalCache()

使用

import LocalCache from '@/utils/cache.js'; //导入

//登录成功,缓存信息  写在需要使用的函数里面
LocalCache.setCache('userInfo', res.data.result);