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
> This feature is not released yet! It will be out soon in [email protected].
84
+
By default, 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.
85
85
86
-
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:
86
+
If you want your modules to be more self-contained or resuable, you can mark it as namespaced with `namespaced: true`. When the module is registered, all of its getters, actions and mutations will be automatically namespaced based on the path the module is registered at. For example:
87
87
88
88
```js
89
-
exportdefault {
90
-
namespace:'account/',
91
-
92
-
// module assets
93
-
state: { ... }, // module state will not be changed by prefix option
popular () { ... } // -> getters['account/posts/popular']
123
+
}
124
+
}
121
125
}
122
126
}
123
127
}
124
-
}
128
+
})
125
129
```
126
130
127
-
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.
131
+
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. Toggling between namespaced or not does not affect the code inside the module.
128
132
129
-
```js
130
-
exportdefault {
131
-
namespace:'prefix/',
133
+
#### Accessing Global Assets in Namespaced Modules
132
134
133
-
getters: {
134
-
// `getters` is localized to this module's getters
135
-
// you can use rootGetters via 4th argument of getters
If you want to use global state and getters, `rootState` and `rootGetters` are passed as the 3rd and 4th arguments to getter functions, and also exposed as properties on the `context` object passed to action functions.
136
+
137
+
To dispatch actions or commit mutations in the global namespace, pass `{ root: true }` as the 3rd argument to `dispatch` and `commit`.
138
+
139
+
```js
140
+
modules: {
141
+
foo: {
142
+
namespaced:true,
143
+
144
+
getters: {
145
+
// `getters` is localized to this module's getters
146
+
// you can use rootGetters via 4th argument of getters
When binding a namespaced module to components with the `mapState`, `mapGetters`, `mapActions` and `mapMutations` helpers, it can get a bit verbose:
176
+
177
+
```js
178
+
computed: {
179
+
...mapState({
180
+
a:state=>state.some.nested.module.a,
181
+
b:state=>state.some.nested.module.b
182
+
})
183
+
},
184
+
methods: {
185
+
...mapActions([
186
+
'some/nested/module/foo',
187
+
'some/nested/module/bar'
188
+
])
189
+
}
190
+
```
191
+
192
+
In such cases, you can pass the module namespace string as the first argument to the helpers so that all bindings are done using that module as the context. The above can be simplified to:
193
+
194
+
```js
195
+
computed: {
196
+
...mapState('some/nested/module', {
197
+
a:state=>state.a,
198
+
b:state=>state.b
199
+
})
200
+
},
201
+
methods: {
202
+
...mapActions('some/nested/module', [
203
+
'foo',
204
+
'bar'
205
+
])
206
+
}
207
+
```
208
+
161
209
#### Caveat for Plugin Developers
162
210
163
211
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