JavaScript开发小技巧之存储loalStorage

2024年07月31日 建站教程

存储loalStorage

export const loalStorageSet = (key, value) => {
    if (!key) return;
    if (typeof value !== 'string') {
        value = JSON.stringify(value);
    }
    window.localStorage.setItem(key, value);
};

获取loalStorage

export const loalStorageGet = (key) => {
    if (!key) return;
    return window.localStorage.getItem(key);
};

删除loalStorage

export const loalStorageRemove = (key) => {
    if (!key) return;
    window.localStorage.removeItem(key);
};

存储sessionStorage

export const sessionStorageSet = (key, value) => {
    if (!key) return;
    if (typeof value !== 'string') {
            value = JSON.stringify(value);
    }
    window.sessionStorage.setItem(key, value)
};

获取sessionStorage

export const sessionStorageGet = (key) => {
    if (!key) return;
    return window.sessionStorage.getItem(key)
};

删除sessionStorage

export const sessionStorageRemove = (key) => {
    if (!key) return;
    window.sessionStorage.removeItem(key)
};

本文链接:http://so.lmcjl.com/news/9612/

展开阅读全文
相关内容