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(); } }
请爱好前端技术的朋友,联系我,有问题大家一起讨论