const state = {
name:'',
age:11
}
const _getters = {}
const _mutations = {}
for (let k in state) {
_getters[k] = state => state[k]
// mutation 以set开头加state名驼峰
_mutations['set'+k.replace(/[a-z]/, (L) => L.toUpperCase())] = (state,payload)=>{
state[k] = payload
}
}
const actions = {
action(context){
context.commit('setName','TOM')
}
}
const getters = {
... _getters,{getter1(){},getter2(){}}
}
const mutations = {
... _mutations,{mutation1(){},mutation2(){}}
}
const store = new Vuex.Store({
state,getters,actions,mutations
})
export default store
网友评论