Skip to content

Commit fe75f42

Browse files
ktsnyyx990803
authored andcommitted
add more concrete description for namespace (#348)
1 parent 2a67103 commit fe75f42

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

docs/en/modules.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,45 @@ const moduleA = {
7171

7272
### Namespacing
7373

74-
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+
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 by prefixing or suffixing their names. And you probably should if you are writing a reusable Vuex module that will be used in unknown environments. For example, we want to create a `todos` module:
75+
76+
``` js
77+
// types.js
78+
79+
// define names of getters, actions and mutations as constants
80+
// and they are prefixed by the module name `todos`
81+
export const DONE_COUNT = 'todos/DONE_COUNT'
82+
export const FETCH_ALL = 'todos/FETCH_ALL'
83+
export const TOGGLE_DONE = 'todos/TOGGLE_DONE'
84+
```
85+
86+
``` js
87+
// modules/todos.js
88+
import * as types from '../types'
89+
90+
// define getters, actions and mutations using prefixed names
91+
const todosModule = {
92+
state: { todos: [] },
93+
94+
getters: {
95+
[types.DONE_COUNT] (state) {
96+
// ...
97+
}
98+
},
99+
100+
actions: {
101+
[types.FETCH_ALL] (context, payload) {
102+
// ...
103+
}
104+
},
105+
106+
mutations: {
107+
[types.TOGGLE_DONE] (state, payload) {
108+
// ...
109+
}
110+
}
111+
}
112+
```
75113

76114
### Dynamic Module Registration
77115

0 commit comments

Comments
 (0)