localStorage增删改查

/**
 * 设置  本地缓存
 */
export function setStorage(key, obj) {
    if (typeof obj === 'string') {
        localStorage.setItem(key, obj);
    } else {
        localStorage.setItem(key, JSON.stringify(obj));
    }
}

/**
 * 获取
 */
export function getStorage(key) {
    let val = localStorage.getItem(key);
    try {
        return JSON.parse(val);
    } catch (e) {
        return val;
    }
}

/**
 * 删除, 如果不传值,删除所有
 */
export function clearStorage(key) {
    if (key) {
        localStorage.removeItem(key);
    } else {
        localStorage.clear();
    }
}

 

posted @ 2017-11-20 14:11  刘浩2561179983  阅读(1842)  评论(0编辑  收藏  举报