You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
house/fount/utils/storage.js

17 lines
1.0 KiB

const storage = { // 定义一个名为 storage 的对象,用于操作浏览器的 localStorage
set(key, value) { // 定义 set 方法,用于存储键值对
localStorage.setItem(key, JSON.stringify(value)); // 将 value 转换为 JSON 字符串后存储到 localStorage 中
},
get(key) { // 定义 get 方法,用于获取指定 key 的值
return localStorage.getItem(key) ? localStorage.getItem(key).replace('"', '').replace('"', '') : ""; // 获取值并移除可能存在的引号,如果不存在则返回空字符串
},
getObj(key) { // 定义 getObj 方法,用于获取指定 key 的值并解析为对象
return localStorage.getItem(key) ? JSON.parse(localStorage.getItem(key)) : null; // 如果存在对应的值,则解析为 JSON 对象,否则返回 null
},
remove(key) { // 定义 remove 方法,用于删除指定 key 的值
localStorage.removeItem(key); // 从 localStorage 中移除指定的 key
}
};
export default storage; // 导出 storage 对象,供其他模块使用