import Vue from
'vue'
import Vuex from
'vuex'
import user from
'./modules/user'
import
global
from
'./modules/global'
import getters from
'./getters'
Vue.
use
(Vuex)
const
store =
new
Vuex.Store({
modules: {
user
},
getters
})
export
default
store
在store文件下的getters.js中引入
const
getters = {
self: state => state.user.self,
token: state => state.user.token,
currentCommunity: (state, getters) => {
let cid = getters.currentCommunityId
return
getters.communities.filter(item => {
return
item.communityId === cid
})
}
}
export
default
getters
在modules文件下的user.js写代码
const
user = {
state:{
self: null,
token:
''
,
},
mutations:{
SET_SELF: (state, self) => {
state.self = self
},
SET_TOKEN: (state, token) => {
state.token = token
}
},
actions:{
login ({ commit }, res) {
commit(
'SET_SELF'
, res.self)
commit(
'SET_TOKEN'
, res.token
}
}
export
default
user