vuex modules模块

1. modules中的state

export default () => {
  return new Vuex.Store({
    modules: {
      a: {
        state: {
          text: 1
        }
      },
      b: {
        state: {
          text: 2
        }
      }
    }
  })
}
computed: {
    textA () {
      return this.$store.state.a.text // 1
    }
  }

或者

...mapState({
  textA: (state) => state.a.text
})

2.modules中的mutation

直接可以在mapMutations中使用modules中的mutation

modules: {
  a: {
    state: {
      text: 1
    },
    mutations: {
      updateText (state, text) {
        console.log('a.state', state)
        state.text = text
      }
    }
  }
}
mounted () {
  this.updateText('123')
},
methods: {
  ...mapMutations(['updateText'])
}

3. 为modules中的mutation加上命名空间

modules: {
  a: {
    namespaced: true,
    state: {
      text: 1
    },
    mutations: {
      updateText (state, text) {
        console.log('a.state', state)
        state.text = text
      }
    }
  }
}

有命名空间时的调用方式

mounted () {
  this['a/updateText']('123')
},
methods: {
  ...mapMutations(['a/updateText'])
}

4. modules中的getter

modules: {
  a: {
    namespaced: true,
    state: {
      text: 1
    },
    getters: {
      textPlus (state) {
        return state.text + 1
      }
    }
  }
}
computed: {
  ...mapGetters({
    textPlus: 'a/textPlus'
  })
}

5. 为modules中的getter调用全局的state

getters: {
  textPlus (state, getters, rootState) {
    return state.text + rootState.count
  }
}

getter调用另一个module的state

getters: {
  textPlus (state, getters, rootState) {
    return state.text + rootState.b.text
  }
}

6. modules中的action 修改全局的mutation

actions: {
  add ({state, commit, rootState}) {
    commit('updateCount', rootState.count)
  }
}

此时,控制台会报错[vuex] unknown local mutation type: updateCount, global type: a/updateCount
需要允许module调用全局的mutation

 add ({state, commit, rootState}) {
    commit('updateCount', {num: 999}, {root: true})
  }

7. modules b 的action调用modules a 的mutation

modules: {
  a: {
    namespaced: true,
    state: {
      text: 1
    },
    mutations: {
      updateText (state, text) {
        console.log('a.state', state)
        state.text = text
      }
    }
  },
  b: {
    actions: {
      testAction ({commit}) {
        commit('a/updateText', 'test text')
      }
    }
  }
}
mounted () {
  this.testAction()
},
methods: {
  ...mapActions(['testAction'])
}

给modules b 加上命名空间

b: {
  namespaced: true,
  state: {
    text: 2
  },
  actions: {
    testAction ({commit}) {
      commit('a/updateText', 'test text', {root: true})
    }
  }
}

此时调用也需要加上命名空间

mounted () {
  this['b/testAction']()
},
methods: {
  ...mapActions(['b/testAction'])
}

module中也可嵌套模块

动态加载module

// main.js
store.registerModule('c', {
  state: {
    text: 3
  }
})

为vuex添加热更新的功能

export default () => {
  const store = new Vuex.Store({
    strict: isDev,
    state: defaultState,
    mutations,
    getters,
    actions
  })
  if (module.hot) {
    module.hot.accept([
      './state/state',
      './mutations/mutations',
      './getters/getters',
      './action/action'
    ], () => {
      const newState = require('./state/state').default
      const newMutations = require('./mutations/mutations').default
      const newGetters = require('./getters/getters').default
      const newActions = require('./action/action').default

      store.hotUpdate({
        state: newState,
        mutations: newMutations,
        getters: newGetters,
        actions: newActions
      })
    })
  }
  return store
}
Vuex中,modules可以用来将store分割为多个模块,每个模块可以拥有自己的state、mutation、action和getter。你可以使用store.registerModule方法来注册模块,例如: ``` // 注册模块 `myModule` store.registerModule('myModule', { // ... }) // 注册嵌套模块 `nested/myModule` store.registerModule(['nested', 'myModule'], { // ... }) ``` 通过将store分割为模块,可以解决当应用变得非常复杂时store对象变得臃肿的问题。每个模块都可以有自己的状态、变化、行为和获取器,甚至可以包含嵌套子模块。例如: ``` const moduleA = { state: { ... }, mutations: { ... }, actions: { ... }, getters: { ... } } const moduleB = { state: { ... }, mutations: { ... }, actions: { ... } } const store = new Vuex.Store({ modules: { a: moduleA, b: moduleB } }) ``` 通过模块化的方式组织store,可以更好地管理和组织复杂的应用程序。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [vuex深入学习 ---- Module](https://blog.csdn.net/qq_42731879/article/details/82766065)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [深入理解vuex2.0 之 modules](https://download.csdn.net/download/weixin_38736721/12769097)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值