美文网首页
获取,设置,删除sessionStorage或localStor

获取,设置,删除sessionStorage或localStor

作者: 我是syq吖 | 来源:发表于2019-07-29 13:31 被阅读0次

    //获取sessionStorage存储数据

    export const getCache = name => {
        if (!name) return
        return window.sessionStorage.getItem(name)
    }
    

    //设置sessionStorage存储数据

    export const setCache = (name, content) => {
        if (!name) return
        if (typeof content !== 'string') {
            content = JSON.stringify(content)
        }
        window.sessionStorage.setItem(name, content)
    }
    

    //删除sessionStorage存储数据

    export const removeCache = (name) => {
        if (!name) return
        window.sessionStorage.removeItem(name)
    }
    

    //获取localStorage存储数据

        if (!name) return
        return window.localStorage.getItem(name)
    }
    

    //设置localStorage存储数据

    export const set_local_cache = (name, content) => {
        if (!name) return
        if (typeof content !== 'string') {
            content = JSON.stringify(content)
        }
        window.localStorage.setItem(name, content)
    }
    

    //删除localStorage存储数据

    export const remove_local_cache = (name) => {
        if (!name) return
        window.localStorage.removeItem(name)
    }
    

    相关文章

      网友评论

          本文标题:获取,设置,删除sessionStorage或localStor

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