import Vue from 'vue'
import Vuex from 'vuex'
Vue.Vue(Vuex);
const store = new Vuex.Store({
state:{
username:uni.getStorageSync('username')?uni.getStorageSync('username'):"未登录用户"
},
mutations:{
MLOGIN(state,userNmae){
uni.setStorageSync('username',userNmae)
state.username = userNmae
},
MLOGOUT(state){
state.username = '未登录用户',
// uni.removeStorage('username')
uni.clearStorageSync();
}
},
actions:{
login(context,userName){ //context是上下文对象
context.commit('MLOGIN',userName)
},
logout(context){
context.commit('MLOGOUT')
}
}
})
export default store
用本地存储来保存vuex的状态,避免刷新时vuex的状态消失
最新推荐文章于 2024-02-07 14:16:31 发布