|
@@ -83,7 +83,7 @@ const moduleA = {
|
|
|
|
|
|
默认情况下,模块内部的 action、mutation 和 getter 是注册在**全局命名空间**的——这样使得多个模块能够对同一 mutation 或 action 作出响应。
|
|
默认情况下,模块内部的 action、mutation 和 getter 是注册在**全局命名空间**的——这样使得多个模块能够对同一 mutation 或 action 作出响应。
|
|
|
|
|
|
-如果希望你的模块具有更高的封装度和复用性,你可以通过添加 `namespaced: true` 的方式使其成为命名空间模块。当模块被注册后,它的所有 getter、action 及 mutation 都会自动根据模块注册的路径调整命名。例如:
|
|
|
|
|
|
+如果希望你的模块具有更高的封装度和复用性,你可以通过添加 `namespaced: true` 的方式使其成为带命名空间的模块。当模块被注册后,它的所有 getter、action 及 mutation 都会自动根据模块注册的路径调整命名。例如:
|
|
|
|
|
|
``` js
|
|
``` js
|
|
const store = new Vuex.Store({
|
|
const store = new Vuex.Store({
|
|
@@ -130,7 +130,7 @@ const store = new Vuex.Store({
|
|
|
|
|
|
启用了命名空间的 getter 和 action 会收到局部化的 `getter`,`dispatch` 和 `commit`。换言之,你在使用模块内容(module assets)时不需要在同一模块内额外添加空间名前缀。更改 `namespaced` 属性后不需要修改模块内的代码。
|
|
启用了命名空间的 getter 和 action 会收到局部化的 `getter`,`dispatch` 和 `commit`。换言之,你在使用模块内容(module assets)时不需要在同一模块内额外添加空间名前缀。更改 `namespaced` 属性后不需要修改模块内的代码。
|
|
|
|
|
|
-#### 在命名空间模块内访问全局内容(Global Assets)
|
|
|
|
|
|
+#### 在带命名空间的模块内访问全局内容(Global Assets)
|
|
|
|
|
|
如果你希望使用全局 state 和 getter,`rootState` 和 `rootGetter` 会作为第三和第四参数传入 getter,也会通过 `context` 对象的属性传入 action。
|
|
如果你希望使用全局 state 和 getter,`rootState` 和 `rootGetter` 会作为第三和第四参数传入 getter,也会通过 `context` 对象的属性传入 action。
|
|
|
|
|
|
@@ -170,9 +170,35 @@ modules: {
|
|
}
|
|
}
|
|
```
|
|
```
|
|
|
|
|
|
|
|
+#### 在带命名空间的模块注册全局 action
|
|
|
|
+
|
|
|
|
+若需要在带命名空间的模块注册全局 action,你可添加 `root: true`,并将这个 action 的定义放在函数 `handler` 中。例如:
|
|
|
|
+
|
|
|
|
+``` js
|
|
|
|
+{
|
|
|
|
+ actions: {
|
|
|
|
+ someOtherAction ({dispatch}) {
|
|
|
|
+ dispatch('someAction')
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ modules: {
|
|
|
|
+ foo: {
|
|
|
|
+ namespaced: true,
|
|
|
|
+
|
|
|
|
+ actions: {
|
|
|
|
+ someAction: {
|
|
|
|
+ root: true,
|
|
|
|
+ handler (namespacedContext, payload) { ... } // -> 'someAction'
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+```
|
|
|
|
+
|
|
#### 带命名空间的绑定函数
|
|
#### 带命名空间的绑定函数
|
|
|
|
|
|
-当使用 `mapState`, `mapGetters`, `mapActions` 和 `mapMutations` 这些函数来绑定命名空间模块时,写起来可能比较繁琐:
|
|
|
|
|
|
+当使用 `mapState`, `mapGetters`, `mapActions` 和 `mapMutations` 这些函数来绑定带命名空间的模块时,写起来可能比较繁琐:
|
|
|
|
|
|
``` js
|
|
``` js
|
|
computed: {
|
|
computed: {
|