vue–vuex 中 Modules 詳解
前言
在Vue中State使用是單一狀態樹結構,應該的所有的狀態都放在state裡面,如果項目比較複雜,那state是一個很大的對象,store對象也將對變得非常大,難於管理。於是Vuex中就存在了另外一個核心概念 modules。本文就來總結 modules 相關知識點。
正文
1 、什麼是模組Modules
Vuex允許我們將store分割成模組(Module), 而每個模組擁有自己的state、getters、mutation、action等,甚至是嵌套子模組——從上至下進行同樣方式的分割。
const moduleA = { state: () => ({ ... }), mutations: { ... }, actions: { ... }, getters: { ... } } const moduleB = { state: () => ({ ... }), mutations: { ... }, actions: { ... } } const store = new Vuex.Store({ modules: { a: moduleA, b: moduleB } }) this.store.state.a // -> 獲得moduleA 的狀態 this.store.state.b // -> 獲得moduleB 的狀態
內部state,模組內部的state是局部的,也就是模組私有的,
內部getter,mutation,action 仍然註冊在全局命名空間內,這樣使得多個模組能夠對同一 mutation 或 action 作出響應。
2、模組內部參數問題
對於模組內部的 mutation 和 getter,接收的第一個參數是模組的局部狀態對象 state。
對於模組內部的 action,局部狀態通過 context.state 暴露出來,根節點狀態則為 context.rootState:
對於模組內部的 getter,根節點狀態會作為第三個參數暴露出來:
const moduleA = { state: () => ({ count:"", }), actions: { //這裡的state為局部狀態,rootState為根節點狀態 incrementIfOddOnRootSum ({ state, commit, rootState }) { if ((state.count + rootState.count) % 2 === 1) { commit('increment') } } } mutations: { // 這裡的 `state` 對象是模組的局部狀態 increment (state) { state.count++ } }, getters: { //這裡的state為局部狀態,rootState為根節點狀態 doubleCount (state) { return state.count * 2 }, sumWithRootCount (state, getters, rootState) { return state.count + rootState.count } } }
3、模組命名空間問題
(1)namespaced: true 使模組成為帶命名空間的模組
當模組被註冊後,它的所有 getter、action 及 mutation 都會自動根據模組註冊的路徑調整命名。
const store = new Vuex.Store({ modules: { account: { namespaced: true, // 模組內容(module assets) 在使用模組內容(module assets)時不需要在同一模組內額外添加空間名前綴。 state: () => ({}), // 模組內的狀態已經是嵌套的了,使用 `namespaced` 屬性不會對其產生影響 getters: { isAdmin() {}, // ->使用: getters['account/isAdmin'], // 你可以使用 getter 的第四個參數來調用 someGetter(state, getters, rootState, rootGetters) { // getters.isAdmin // rootGetters.someOtherGetter }, }, actions: { login() {}, // ->使用: dispatch('account/login') // 你可以使用 action 的第四個參數來調用 //若需要在全局命名空間內分發 action 或提交 mutation,將 { root: true } 作為第三參數傳給 dispatch 或 commit 即可 someAction({ dispatch, commit, getters, rootGetters }) { // getters.isAdmin; // rootGetters.someGetter; // dispatch("someOtherAction"); // dispatch("someOtherAction", null, { root: true }); // commit("someMutation"); // commit("someMutation", null, { root: true }); }, someOtherAction(ctx, payload) {}, // 若需要在帶命名空間的模組註冊全局 action,你可添加 root: true,並將這個 action 的定義放在函數 handler 中。 otherAction: { root: true, handler(namespacedContext, payload) {}, // -> 'someAction' }, }, mutations: { login() {}, // ->使用: commit('account/login') }, // 嵌套模組 modules: { // 繼承父模組的命名空間 myPage: { state: () => ({}), getters: { profile() {}, // -> 使用:getters['account/profile'] }, }, // 進一步嵌套命名空間 posts: { namespaced: true, state: () => ({}), getters: { popular() {}, // -> 使用:getters['account/posts/popular'] }, }, }, }, }, });
(2)帶命名空間的綁定函數的使用
當使用 mapState, mapGetters, mapActions 和 mapMutations 這些函數來綁定帶命名空間的模組時,寫起來可能比較繁瑣:
computed: { ...mapState({ a: state => state.some.nested.module.a, b: state => state.some.nested.module.b }) }, methods: { ...mapActions([ 'some/nested/module/foo', // -> this['some/nested/module/foo']() 'some/nested/module/bar' // -> this['some/nested/module/bar']() ]) }
createNamespacedHelpers 創建基於某個命名空間輔助函數,它返回一個對象,對象里有新的綁定在給定命名空間值上的組件綁定輔助函數。
import { createNamespacedHelpers } from 'vuex' const { mapState, mapActions } = createNamespacedHelpers('some/nested/module') export default { computed: { // 在 `some/nested/module` 中查找 ...mapState({ a: state => state.a, b: state => state.b }) }, methods: { // 在 `some/nested/module` 中查找 ...mapActions([ 'foo', 'bar' ]) } }
4、模組動態註冊
在 store 創建之後,你可以使用 store.registerModule 方法註冊模組
import Vuex from 'vuex' const store = new Vuex.Store({ /* 選項 */ }) // 註冊模組 `myModule` store.registerModule('myModule', { // ... }) // 註冊嵌套模組 `nested/myModule` store.registerModule(['nested', 'myModule'], { // ... })
之後就可以通過 store.state.myModule 和 store.state.nested.myModule 訪問模組的狀態。
也可以使用 store.unregisterModule(moduleName) 來動態卸載模組。注意,你不能使用此方法卸載靜態模組(即創建 store 時聲明的模組)。
可以通過 store.hasModule(moduleName) 方法檢查該模組是否已經被註冊到 store。
寫在最後
以上就是本文的全部內容,希望給讀者帶來些許的幫助和進步,方便的話點個關注,小白的成長之路會持續更新一些工作中常見的問題和技術點。