Vuex根据state 生成 getters 、mutation
2021-12-20 本文已影响0人
笨小孩81
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