美文网首页
Vuex根据state 生成 getters 、mutation

Vuex根据state 生成 getters 、mutation

作者: 笨小孩81 | 来源:发表于2021-12-20 11:01 被阅读0次
    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
    

    相关文章

      网友评论

          本文标题:Vuex根据state 生成 getters 、mutation

          本文链接:https://www.haomeiwen.com/subject/nwfafrtx.html