vuex中Modules的使用詳解
前言
在Vue中State使用是單一狀態(tài)樹結構,應該的所有的狀態(tài)都放在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 的狀態(tài)
this.store.state.b // -> 獲得moduleB 的狀態(tài)內部state,模塊內部的state是局部的,也就是模塊私有的,
內部getter,mutation,action 仍然注冊在全局命名空間內,這樣使得多個模塊能夠對同一 mutation 或 action 作出響應。
2、模塊內部參數(shù)問題
對于模塊內部的 mutation 和 getter,接收的第一個參數(shù)是模塊的局部狀態(tài)對象 state。
對于模塊內部的 action,局部狀態(tài)通過 context.state 暴露出來,根節(jié)點狀態(tài)則為 context.rootState:
對于模塊內部的 getter,根節(jié)點狀態(tài)會作為第三個參數(shù)暴露出來:
const moduleA = {
state: () => ({
count:"",
}),
actions: {
//這里的state為局部狀態(tài),rootState為根節(jié)點狀態(tài)
incrementIfOddOnRootSum ({ state, commit, rootState }) {
if ((state.count + rootState.count) % 2 === 1) {
commit('increment')
}
}
}
mutations: {
// 這里的 `state` 對象是模塊的局部狀態(tài)
increment (state) {
state.count++
}
},
getters: {
//這里的state為局部狀態(tài),rootState為根節(jié)點狀態(tài)
doubleCount (state) {
return state.count * 2
},
sumWithRootCount (state, getters, rootState) {
return state.count + rootState.count
}
}
}3、模塊命名空間問題
(1)namespaced: true 使模塊成為帶命名空間的模塊
當模塊被注冊后,它的所有 getter、action 及 mutation 都會自動根據(jù)模塊注冊的路徑調整命名。
const store = new Vuex.Store({
modules: {
account: {
namespaced: true,
// 模塊內容(module assets) 在使用模塊內容(module assets)時不需要在同一模塊內額外添加空間名前綴。
state: () => ({}), // 模塊內的狀態(tài)已經是嵌套的了,使用 `namespaced` 屬性不會對其產生影響
getters: {
isAdmin() {}, // ->使用: getters['account/isAdmin'],
// 你可以使用 getter 的第四個參數(shù)來調用
someGetter(state, getters, rootState, rootGetters) {
// getters.isAdmin
// rootGetters.someOtherGetter
},
},
actions: {
login() {}, // ->使用: dispatch('account/login')
// 你可以使用 action 的第四個參數(shù)來調用
//若需要在全局命名空間內分發(fā) action 或提交 mutation,將 { root: true } 作為第三參數(shù)傳給 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 的定義放在函數(shù) 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)帶命名空間的綁定函數(shù)的使用
當使用 mapState, mapGetters, mapActions 和 mapMutations 這些函數(shù)來綁定帶命名空間的模塊時,寫起來可能比較繁瑣:
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 創(chuàng)建基于某個命名空間輔助函數(shù),它返回一個對象,對象里有新的綁定在給定命名空間值上的組件綁定輔助函數(shù)。
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、模塊動態(tài)注冊
在 store 創(chuàng)建之后,你可以使用 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 訪問模塊的狀態(tài)。
也可以使用 store.unregisterModule(moduleName) 來動態(tài)卸載模塊。注意,你不能使用此方法卸載靜態(tài)模塊(即創(chuàng)建 store 時聲明的模塊)。
可以通過 store.hasModule(moduleName) 方法檢查該模塊是否已經被注冊到 store。
寫在最后
到此這篇關于vuex中Modules的使用詳解的文章就介紹到這了,更多相關vuex Modules內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue的filters(本地)或filter(全局)過濾常用數(shù)據(jù)類型解析
這篇文章主要介紹了Vue的filters(本地)或filter(全局)過濾常用數(shù)據(jù)類型,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
Vue使用Tinymce富文本自定義toolbar按鈕的實踐
本文主要介紹了Vue使用Tinymce富文本自定義toolbar按鈕,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12

