目录结构
index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
// $store.state.user.userid
// $store.commit("user/setUserId", 123456);
// user 用户模块!!!
import user from "./modules/user"
export default new Vuex.Store({
modules: {
user
},
state: {
},
mutations: {
},
actions: {
}
})
user.js
//用户模块
export default {
namespaced: true, // 开起命名空间。
state: {
//用户ID
userid: 504334023,
name: "秃驴"
},
actions: {
setUser({ commit }, val) {
commit("set_user", val)
}
},
mutations: {
setUserId(state, val) {
state.userid = val;
}
},
getters: {
getUserId(state) {
return state.userid + '秃驴飞'
}
}
}
使用方法
获取
this.$store.state.user.userid
设置
this.$store.commit("user/setUserId", 123456);