Skip to content

Commit 2a4dbe1

Browse files
authored
docs(zh): update b40ee5d...7409ad4 (#1772)
1 parent 7409ad4 commit 2a4dbe1

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

docs/zh/api/README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ const store = new Vuex.Store({ ...options })
176176
177177
### subscribe
178178
179-
- `subscribe(handler: Function): Function`
179+
- `subscribe(handler: Function, options?: Object): Function`
180180
181181
订阅 store 的 mutation。`handler` 会在每个 mutation 完成后调用,接收 mutation 和经过 mutation 后的状态作为参数:
182182
@@ -187,13 +187,18 @@ const store = new Vuex.Store({ ...options })
187187
})
188188
```
189189
190+
默认情况下,新的处理函数会被添加到其链的尾端,因此它会在其它之前已经被添加了的处理函数之后执行。这一行为可以通过向 `options` 添加 `prepend: true` 来覆写,即把处理函数添加到其链的最开始。
191+
192+
``` js
193+
store.subscribe(handler, { prepend: true })
194+
```
190195
 要停止订阅,调用此方法返回的函数即可停止订阅。
191196
192197
通常用于插件。[详细介绍](../guide/plugins.md)
193198
194199
### subscribeAction
195200
196-
- `subscribeAction(handler: Function): Function`
201+
- `subscribeAction(handler: Function, options?: Object): Function`
197202
198203
> 2.5.0 新增
199204
@@ -206,6 +211,12 @@ const store = new Vuex.Store({ ...options })
206211
})
207212
```
208213
214+
默认情况下,新的处理函数会被添加到其链的尾端,因此它会在其它之前已经被添加了的处理函数之后执行。这一行为可以通过向 `options` 添加 `prepend: true` 来覆写,即把处理函数添加到其链的最开始。
215+
216+
``` js
217+
store.subscribeAction(handler, { prepend: true })
218+
```
219+
209220
要停止订阅,调用此方法返回的函数即可停止订阅。
210221
211222
> 3.1.0 新增
@@ -223,7 +234,20 @@ const store = new Vuex.Store({ ...options })
223234
})
224235
```
225236
226-
该功能常用于插件。[详细介绍](../guide/plugins.md)
237+
> 3.4.0 新增
238+
239+
自 3.4.0 起,`subscribeAction` 也可以指定一个 `error` 处理函数以捕获分发 action 的时候被抛出的错误。该函数会从第三个参数接收到一个 `error` 对象。
240+
241+
``` js
242+
store.subscribeAction({
243+
error: (action, state, error) => {
244+
console.log(`error action ${action.type}`)
245+
console.error(error)
246+
}
247+
})
248+
```
249+
250+
`subscribeAction` 方法常用于插件。[详细介绍](../guide/plugins.md)
227251
228252
### registerModule
229253

docs/zh/guide/hot-reload.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,50 @@ if (module.hot) {
4242
```
4343

4444
参考热重载示例 [counter-hot](https://github.com/vuejs/vuex/tree/dev/examples/counter-hot)
45+
46+
## 动态模块热重载
47+
48+
如果你仅使用模块,你可以使用 `require.context` 来动态地加载或热重载所有的模块。
49+
50+
```js
51+
// store.js
52+
import Vue from 'vue'
53+
import Vuex from 'vuex'
54+
55+
// 加载所有模块。
56+
function loadModules() {
57+
const context = require.context("./modules", false, /([a-z_]+)\.js$/i)
58+
59+
const modules = context
60+
.keys()
61+
.map((key) => ({ key, name: key.match(/([a-z_]+)\.js$/i)[1] }))
62+
.reduce(
63+
(modules, { key, name }) => ({
64+
...modules,
65+
[name]: context(key).default
66+
}),
67+
{}
68+
)
69+
70+
return { context, modules }
71+
}
72+
73+
const { context, modules } = loadModules()
74+
75+
Vue.use(Vuex)
76+
77+
const store = new Vuex.Store({
78+
modules
79+
})
80+
81+
if (module.hot) {
82+
// 在任何模块发生改变时进行热重载。
83+
module.hot.accept(context.id, () => {
84+
const { modules } = loadModules()
85+
86+
store.hotUpdate({
87+
modules
88+
})
89+
})
90+
}
91+
```

docs/zh/guide/plugins.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ const logger = createLogger({
109109
   // 顺便,`mutation` 是个 { type, payload } 对象
110110
   return mutation.type !== "aBlacklistedMutation"
111111
},
112+
actionFilter (action, state) {
113+
// 和 `filter` 一样,但是是针对 action 的
114+
// `action` 的格式是 `{ type, payload }`
115+
return action.type !== "aBlacklistedAction"
116+
},
112117
transformer (state) {
113118
// 在开始记录之前转换状态
114119
// 例如,只返回指定的子树
@@ -119,6 +124,12 @@ const logger = createLogger({
119124
// 我们可以按任意方式格式化
120125
return mutation.type
121126
},
127+
actionTransformer (action) {
128+
// 和 `mutationTransformer` 一样,但是是针对 action 的
129+
return action.type
130+
},
131+
logActions: true, // 记录 action 日志
132+
logMutations: true, // 记录 mutation 日志
122133
logger: console, // 自定义 console 实现,默认为 `console`
123134
})
124135
```

0 commit comments

Comments
 (0)