目录Storage本地化存储localStoragesessionStorageStrorage本地存储实例在model文件夹下面新建一个storage.js创建storeStora
存储优点:
const storage = {
set(key,value){
sessionStorage.setItem(key,JSON.stringify(value));
},
get(key){
return JSON.parse(sessionStorage.getItem(key));
},
getForIndex(index){
return sessionStorage.key(index)
},
geTKEys(){
let items = this.getAll();
let keys = [];
for(let index = 0;index<items.length;index++){
keys.push(items[index].key);
}
},
getLength(){
return sessionStorage.length;
},
getSupport(){
return (typeof (Storage) !== "undefined")?true:false;
},
remove(key){
sessionStorage.removeItem(key);
},
removeAll(){
sessionStorage.clear();
},
getAll(){
let len = sessionStorage.length;
let arr = [];
for(let i=0;i<len.length;i++){
const getKey = sessionStorage.key(i);
const getVal = sessionStorage.getItem(getKey);
arr[i] = {
"key":getKey,
"val":getVal,
}
}
return arr;
},
};
export default storage;
import Vue from "vue";
import Vuex from "vuex";
import storage from "@/model/storage";
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
// 存储token
token: '',
userName: '',
roleId:'',
staticRoute:[]//这个是本地路由
},
actions: {
setuserInfoFun(context, name){
context.commit('setuserInfo', name);
},
set_tokenFun(context, token){
context.commit('set_token', token)
},
del_tokenFun(context){
context.commit('del_token')
},
set_roleIdFun(context,id){
context.commit('set_roleId',id)
},
set_routerFun(context,route){
context.commit('set_router',route)
}
},
// 计算属性
mutations: {
// 修改token,并将token存入localStorage
set_token(state, token){
state.token = token;
storage.set('token', token);
},
del_token(state){
state.token = "";
storage.remove('token');
},
setuserInfo(state, userName){
state.userName = userName;
},
set_roleId(state,id){
state.roleId = id;
},
set_router(state,router){
state.staticRoute = router;
storage.set('route', router);
}
}
});
export default store;
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: Vue中的Strorage本地化存储详解
本文链接: https://lsjlt.com/news/147755.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0