封装localstorage---storage.js
作者:互联网
/**
* localStorage and sessionStorage basic operation
*/
const ls = localStorage;
const ss = sessionStorage;
const db = {
ls: {
get(key) {
try {
return JSON.parse(ls.getItem(key));
} catch (err) {
return ls.getItem(key);
}
},
set(key, value) {
ls.setItem(key, JSON.stringify(value));
},
remove(key) {
ls.removeItem(key);
},
clear() {
ls.clear();
}
},
ss: {
get(key) {
try {
return JSON.parse(ss.getItem(key));
} catch (err) {
return ss.getItem(key);
}
},
set(key, value) {
ss.setItem(key, JSON.stringify(value));
},
remove(key) {
ss.removeItem(key);
},
clear() {
ss.clear();
}
}
};
export default db;
//........使用
let username = this.$db.ls.get("username");
let password = this.$db.ls.get("password");
标签:return,getItem,ss,storage,---,JSON,localstorage,ls,key 来源: https://www.cnblogs.com/bwnnxxx123/p/15646316.html