美文网首页前端攻城狮
封装localStorage操作

封装localStorage操作

作者: 黄一倚 | 来源:发表于2019-04-23 20:20 被阅读2次
    /**
     * 存储localStorage
     */
    export const setLocalStorage = (name, content) => {
      if (!name) return
      if (typeof content !== 'string') {
        content = JSON.stringify(content)
      }
      window.localStorage.setItem(name, content)
    }
    
    /**
     * 获取localStorage
     */
    export const getLocalStorage = name => {
      if (!name) return
      return window.localStorage.getItem(name)
    }
    
    /**
     * 删除localStorage
     */
    export const removeLocalStorage = name => {
      if (!name) return
      window.localStorage.removeItem(name)
    }
    
    

    相关文章

      网友评论

        本文标题:封装localStorage操作

        本文链接:https://www.haomeiwen.com/subject/avwbgqtx.html