Browse Source

[DOC ZH-CN EN] add description for feature(register global actions in namespaced modules) (#1199)

* [DOC ZH-CN EN] add description for feature(register global actions in namespaced modules)

* Update modules.md

* Update modules.md
Jiahui.Liang 7 năm trước cách đây
mục cha
commit
f5ff95eafd
2 tập tin đã thay đổi với 55 bổ sung3 xóa
  1. 26 0
      docs/en/modules.md
  2. 29 3
      docs/zh-cn/modules.md

+ 26 - 0
docs/en/modules.md

@@ -170,6 +170,32 @@ modules: {
 }
 ```
 
+#### Register Global Action in Namespaced Modules
+
+If you want to register global actions in namespaced modules, you can mark it with `root: true` and place the action definition to function `handler`. For example:
+
+``` js
+{
+  actions: {
+    someOtherAction ({dispatch}) {
+      dispatch('someAction')
+    }
+  },
+  modules: {
+    foo: {
+      namespaced: true,
+
+      actions: {
+        someAction: {
+          root: true,
+          handler (namespacedContext, payload) { ... } // -> 'someAction'
+        }
+      }
+    }
+  }
+}
+```
+
 #### Binding Helpers with Namespace
 
 When binding a namespaced module to components with the `mapState`, `mapGetters`, `mapActions` and `mapMutations` helpers, it can get a bit verbose:

+ 29 - 3
docs/zh-cn/modules.md

@@ -83,7 +83,7 @@ const moduleA = {
 
 默认情况下,模块内部的 action、mutation 和 getter 是注册在**全局命名空间**的——这样使得多个模块能够对同一 mutation 或 action 作出响应。
 
-如果希望你的模块具有更高的封装度和复用性,你可以通过添加 `namespaced: true` 的方式使其成为命名空间模块。当模块被注册后,它的所有 getter、action 及 mutation 都会自动根据模块注册的路径调整命名。例如:
+如果希望你的模块具有更高的封装度和复用性,你可以通过添加 `namespaced: true` 的方式使其成为命名空间模块。当模块被注册后,它的所有 getter、action 及 mutation 都会自动根据模块注册的路径调整命名。例如:
 
 ``` js
 const store = new Vuex.Store({
@@ -130,7 +130,7 @@ const store = new Vuex.Store({
 
 启用了命名空间的 getter 和 action 会收到局部化的 `getter`,`dispatch` 和 `commit`。换言之,你在使用模块内容(module assets)时不需要在同一模块内额外添加空间名前缀。更改 `namespaced` 属性后不需要修改模块内的代码。
 
-#### 在命名空间模块内访问全局内容(Global Assets)
+#### 在命名空间模块内访问全局内容(Global Assets)
 
 如果你希望使用全局 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
 computed: {