新建storage.js:
export const local={
set(key,value){
localStorage.setItem(key,JSON.stringify(value));
},
get(key){
return JSON.parse(localStorage.getItem(key));
},
remove(key){
localStorage.removeItem(key);
}
}
export const session={
set(key,value){
sessionStorage.setItem(key,JSON.stringify(value));
},
get(key){
return JSON.parse(sessionStorage.getItem(key));
},
remove(key){
sessionStorage.removeItem(key);
}
}
组件中按需引入:
import {local} from "../../utils/storage"
//使用
local.set("userName", this.ruleForm.username);