Modules
1.什麼是
module
?this
背景:在Vue
中State
使用是單一狀態樹結構,應該的全部的狀態都放在state
裏面,若是項目比較複雜,那state
是一個很大的對象,store
對象也將對變得很是大,難於管理。code
module
:能夠讓每個模塊擁有本身的state
、mutation
、action
、getters
,使得結構很是清晰,方便管理。對象
2.怎麼用
module
?get
通常結構it
const moduleA = { state: { ... }, mutations: { ... }, actions: { ... }, getters: { ... } } const moduleB = { state: { ... }, mutations: { ... }, actions: { ... } } const store = new Vuex.Store({ modules: { a: moduleA, b: moduleB})
模塊內部的數據:①內部state
,模塊內部的state
是局部的,也就是模塊私有的,好比是car.js
模塊state
中的list
數據,咱們要經過this.$store.state.car.list
獲取;②內部getter
、mutation
和action
,仍然註冊在全局命名空間內,這是爲了多模塊能夠同時響應同一mutation
;this.$store.state.car.carGetter
的結結果是undefined
,而經過this.$store.state.carGetter
則能夠拿到。io
傳參:getters
====({state
(局部狀態),getters
(全局getters
對象),roosState
(根狀態)});actions
====({state
(局部狀態),commit
,roosState
(根狀態)}).module