客户端存储操作模块

/**
 * 客户端存储操作模块
 */
export default {
  /**
   *  以字符串形式保存到客户端存储
   */
  setStr: function (key, value) {
    if (!key) return
    typeof value === 'object' && (value = JSON.stringify(value))
    window.sessionStorage.setItem(key, value)
  },
  /**
   *  以原始类型保存到客户端存储
   */
  set: function (key, value) {
    if (!key) return
    value = JSON.stringify(value)
    window.sessionStorage.setItem(key, value)
  },
  /**
   *  获取原始类型数据
   */
  get: function (key) {
    if (!key) return null
    return JSON.parse(window.sessionStorage.getItem(key))
  },
  /**
   *  获取字符串类型数据
   */
  getStr: function (key) {
    if (!key) return null
    return window.sessionStorage.getItem(key)
  },
  remove: function (key) {
    return window.sessionStorage.removeItem(key)
  },
  /**
   *  从LocalStorage获取字符串类型数据
   */
  getLocalStr: function (key) {
    if (!key) return null
    return window.localStorage.getItem(key)
  },
  /**
   *  以字符串形式保存到LocalStorage
   */
  setLocalStr: function (key, value) {
    if (!key) return
    typeof value === 'object' && (value = JSON.stringify(value))
    window.localStorage.setItem(key, value)
  },
  removeLocal: function (key) {
    return window.localStorage.removeItem(key)
  }
}
原文地址:https://www.cnblogs.com/ympjsc/p/12304387.html