资讯专栏INFORMATION COLUMN

localStorage、sessionStorage ES6 简单封装 Vue

Ilikewhite / 623人阅读

摘要:简单封装检测是否支持不支持设置值必须参数,属性非必须参数,属性值只能为字符串获取值必须参数,属性只能为字符串移除值必须参数,属性只能为字符串移除所有判断类型必须参数,判断的值过滤值必须参数,过滤的值使用方式

localStorage、sessionStorage ES6简单封装:
class Store {
  constructor (store) {
    // 检测是否支持localstorage
    if (!store) {
      console.log("不支持localStorage")
      return
    }
    this._store = store
  }

  /**
   * @function 设置值
   * @param {string} _k 必须参数,属性
   * @param {any} _v 非必须参数,属性值
   */
  setItem (_k, _v) {
    if (!this._store) return
    let kType = this.getType(_k)
    if (kType === "string") {
      this._store.setItem(_k, this.filterValue(_v))
    } else {
      console.log("key只能为字符串!")
    }
  }

  /**
   * @function 获取值
   * @param {string} _k 必须参数,属性
   */
  getItem (_k) {
    if (!this._store) return
    let res
    let kType = this.getType(_k)
    if (kType === "string") {
      res = this._store.getItem(_k)
    } else {
      console.log("key只能为字符串!")
    }
    return res
  }

  /**
   * @function 移除值
   * @param {string} _k 必须参数,属性
   */
  removeItem (_k) {
    if (!this._store) return
    let kType = this.getType(_k)
    if (kType === "string") {
      res = this._store.removeItem(_k)
    } else {
      console.log("key只能为字符串!")
    }
  }

  /**
   * @function 移除所有
   */
  clear () {
    if (!this._store) return
    this._store.clear()
  }

  /**
   * @function 判断类型
   * @param {any} para 必须参数,判断的值
   */
  getType (para) {
    let type = typeof para
    if (type === "number" && isNaN(para)) return "NaN"
    if (type !== "object") return type
    return Object.prototype.toString
      .call(para)
      .replace(/[[]]/g, "") // eslint-disable-line
      .split(" ")[1]
      .toLowerCase()
  }

  /**
   * @function 过滤值
   * @param {any} val 必须参数,过滤的值
   */
  filterValue (val) {
    let vType = this.getType(val)
    let nullVal = ["null", "undefined", "NaN"]
    let stringVal = ["boolen", "number", "string"]
    if (nullVal.indexOf(vType) >= 0) return ""
    if (stringVal.indexOf(vType) >= 0) return val
    return JSON.stringify(val)
  }
}

class LocalStorage extends Store {
  constructor (store) { // eslint-disable-line
    super(store)
  }
  WX_USER_ID = "WX_USER_ID"
}

class SessionStorage extends Store {
  constructor (store) { // eslint-disable-line
    super(store)
  }
  WX_SSO_TITLE = "WX_SSO_TITLE"
}

const lStorage = new LocalStorage(window.localStorage || localStorage)
const sStorage = new SessionStorage(window.sessionStorage || sessionStorage)

export {
  lStorage,
  sStorage
}
使用方式:
import { lStorage, sStorage } from "./storage.js"

lStorage.setItem(lStorage.WX_USER_ID, ["val"])
lStorage.getItem(lStorage.WX_USER_ID) // ["val"]

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/105237.html

相关文章

  • 用Class写一个记住用户离开位置的js插件

    摘要:咱们为即将写的库起个名字为,开始就是如下的样子啦处理传进来的参数我们需要在类的构造函数中接收参数,并覆盖默认参数。 前言 常见的js插件都很少使用ES6的class,一般都是通过构造函数,而且常常是手写CMD、AMD规范来封装一个库,比如这样: // 引用自:https://www.jianshu.com/p/e65c246beac1 ;(function(undefined) { ...

    haobowd 评论0 收藏0
  • 用Class写一个记住用户离开位置的js插件

    摘要:咱们为即将写的库起个名字为,开始就是如下的样子啦复制代码处理传进来的参数我们需要在类的构造函数中接收参数,并覆盖默认参数。 前言常见的js插件都很少使用ES6的class,一般都是通过构造函数,而且常常是手写CMD、AMD规范来封装一个库,比如这样: // 引用自:https://www.jianshu.com/p/e65... (function(undefined) { use s...

    xialong 评论0 收藏0
  • 用Class写一个记住用户离开位置的js插件

    摘要:咱们为即将写的库起个名字为,开始就是如下的样子啦复制代码处理传进来的参数我们需要在类的构造函数中接收参数,并覆盖默认参数。 前言常见的js插件都很少使用ES6的class,一般都是通过构造函数,而且常常是手写CMD、AMD规范来封装一个库,比如这样: // 引用自:https://www.jianshu.com/p/e65... (function(undefined) { use s...

    Bryan 评论0 收藏0
  • 用Class写一个记住用户离开位置的js插件

    摘要:咱们为即将写的库起个名字为,开始就是如下的样子啦复制代码处理传进来的参数我们需要在类的构造函数中接收参数,并覆盖默认参数。 前言常见的js插件都很少使用ES6的class,一般都是通过构造函数,而且常常是手写CMD、AMD规范来封装一个库,比如这样: // 引用自:https://www.jianshu.com/p/e65... (function(undefined) { use s...

    WilsonLiu95 评论0 收藏0
  • vue项目开发过程常见问题

    摘要:更新时间这个问题是实例内单组件的必须返回一个对象如下为什么要一个数据对象呢官方解释如下必须声明为返回一个初始数据对象的函数,因为组件可能被用来创建多个实例。 更新时间:2018-07-29 1.data functions should return an object // 这个问题是 Vue 实例内,单组件的data必须返回一个对象;如下 export default {...

    Apollo 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<