8000 docs: modules · poorprogrammer/vuejs.org@2a0263f · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a0263f

Browse files
committed
docs: modules
1 parent 661f6c0 commit 2a0263f

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

docs/en/modules.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,89 @@
11
# Modules
2+
3+
Due to using a single state tree, all state of our application is contained inside one big object. However as our application grows in scale, the store can get really bloated.
4+
5+
To help with that, Vuex allows us to divide our store into **modules**. Each module can contain its own state, mutations, actions, getters, and even nested modules - it's fractal all the way down:
6+
7+
``` js
8+
const moduleA = {
9+
state: { ... },
10+
mutations: { ... },
11+
actions: { ... }
12+
}
13+
14+
const moduleB = {
15+
state: { ... },
16+
mutations: { ... },
17+
actions: { ... }
18+
}
19+
20+
const store = new Vuex.Store({
21+
modules: {
22+
a: moduleA,
23+
b: moduleB
24+
}
25+
})
26+
27+
store.state.a // -> moduleA's state
28+
store.state.b // -> moduleB's state
29+
```
30+
31+
### Module Local State
32+
33+
Inside a module's mutations and getters, The first argument received will be **the module's local state** instead of root state:
34+
35+
``` js
36+
cosnt moduleA = {
37+
state: { count: 0 },
38+
mutations: {
39+
increment: (state, rootState) {
40+
// state is the local module state
41+
state.count++
42+
43+
// rootState is passed as the second argument, but you should not
44+
// mutate it from within a module.
45+
}
46+
},
47+
48+
getters: {
49+
doubleCount (state) {
50+
return state.count * 2
51+
}
52+
}
53+
}
54+
```
55+
56+
Similarly, inside module actions, `context.state` will expose the local state, and root state will be exposed as `context.rootState`:
57+
58+
``` js
59+
const moduleA = {
60+
// ...
61+
actions: {
62+
incrementIfOdd ({ state, commit }) {
63+
if (state.count % 2 === 1) {
64+
commit('increment')
65+
}
66+
}
67+
}
68+
}
69+
```
70+
71+
### Namespacing
72+
73+
Note that actions, mutations and getters inside modules are still registered under the **global namespace** - this allows multiple modules to react to the same mutation/action type. You can namespace the module assets yourself to avoid name clashing, and you probably should if you are writing a reusable Vuex module that will be used in unknown environments.
74+
75+
### Dynamic Module Registration
76+
77+
You can register a module **after** the store has been created with the `store.reigsterModule` method:
78+
79+
``` js
80+
store.registerModule('myModule', {
81+
// ...
82+
})
83+
```
84+
85+
The module's state will be exposed as `store.state.myModule`.
86+
87+
Dynamic module registration makes it possible for other Vue plugins to also leverage Vuex for state management by attaching a module to the application's store. For example, the [`vuex-router-sync`](https://github.com/vuejs/vuex-router-sync) library integrates vue-router with vuex by managing the application's route state in a dynamically attached module.
88+
89+
You can also remove a dynamically registered module with `store.unregisterModule(moduleName)`. Note you cannot remove static modules (declared at store creation) with this method.

0 commit comments

Comments
 (0)
0