You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* refactoring: adding module tree model
* add namespace if module namepsace option is specified
* hot reload namespace
* localize getters, dispatch and commit if module is namespaced
* add error message for unknown local action/mutation
* namespace option only accepts string value
* add more test for local getters, dispatch and commit
* update docs for namespace options
* add a caveat for plugin developers
* add more tests
* add typings for module namespace feature
* use normal object for local getters to compat with root getters
* remove unused this._options
* use root dispatch and commit if there is no namespace
Copy file name to clipboardExpand all lines: docs/en/modules.md
+75-24Lines changed: 75 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -81,42 +81,93 @@ const moduleA = {
81
81
82
82
### Namespacing
83
83
84
-
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:
84
+
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 probably should namespace your Vuex module if you are writing a reusable one that will be used in unknown environments. To support namespacing for avoiding name clashing, Vuex provides `namespace` option. If you specify string value to `namespace` option, module assets types are prefixed by the given value:
85
85
86
86
```js
87
-
// types.js
87
+
exportdefault {
88
+
namespace:'account/',
88
89
89
-
// define names of getters, actions and mutations as constants
90
-
// and they are prefixed by the module name `todos`
91
-
exportconstDONE_COUNT='todos/DONE_COUNT'
92
-
exportconstFETCH_ALL='todos/FETCH_ALL'
93
-
exportconstTOGGLE_DONE='todos/TOGGLE_DONE'
90
+
// module assets
91
+
state: { ... }, // module state will not be changed by prefix option
popular () { ... } // -> getters['account/posts/popular']
119
+
}
120
+
}
121
+
}
122
+
}
94
123
```
95
124
96
-
```js
97
-
// modules/todos.js
98
-
import*astypesfrom'../types'
125
+
Namespaced getters and actions will receive localized `getters`, `dispatch` and `commit`. In other words, you can use the module assets without writing prefix in the same module. If you want to use the global ones, `rootGetters` is passed to the 4th argument of getter functions and the property of the action context. In addition, `dispatch` and `commit` receives `root` option on their last argument.
99
126
100
-
// define getters, actions and mutations using prefixed names
101
-
consttodosModule= {
102
-
state: { todos: [] },
127
+
```js
128
+
exportdefault {
129
+
namespace:'prefix/',
103
130
104
131
getters: {
105
-
[types.DONE_COUNT] (state) {
106
-
// ...
107
-
}
132
+
// `getters` is localized to this module's getters
133
+
// you can use rootGetters via 4th argument of getters
You may care about unpredictable namespacing for your modules when you create a [plugin](plugins.md) that provides the modules and let users add them to a Vuex store. Your modules will be also namespaced if the plugin users add your modules under a namespaced module. To adapt this situation, you may need to receive a namespace value via your plugin option:
0 commit comments