@@ -40,9 +40,7 @@ sidebar: auto
40
40
41
41
- 类型: ` { [type: string]: Function } `
42
42
43
- 在 store 上注册 action。处理函数总是接受 ` context ` 作为第一个参数,` payload ` 作为第二个参数(可选)。
44
-
45
- ` context ` 对象包含以下属性:
43
+ 在 store 上注册 action。处理函数总是接受 ` context ` 作为第一个参数,` context ` 对象包含以下属性:
46
44
47
45
``` js
48
46
{
@@ -67,15 +65,15 @@ sidebar: auto
67
65
68
66
```
69
67
state, // 如果在模块中定义则为模块的局部状态
70
- getters, // 等同于 store.getters
68
+ getters // 等同于 store.getters
71
69
```
72
70
73
71
当定义在一个模块里时会特别一些:
74
72
75
73
```
76
74
state, // 如果在模块中定义则为模块的局部状态
77
- getters, // 等同于 store. getters
78
- rootState // 等同于 store. state
75
+ getters, // 当前模块的局部 getters
76
+ rootState, // 全局 state
79
77
rootGetters // 所有 getters
80
78
```
81
79
@@ -89,12 +87,12 @@ sidebar: auto
89
87
90
88
包含了子模块的对象,会被合并到 store,大概长这样:
91
89
92
- ``` js
90
+ ``` js
93
91
{
94
92
key: {
95
93
state,
96
94
namespaced? ,
97
- mutations,
95
+ mutations? ,
98
96
actions? ,
99
97
getters? ,
100
98
modules?
@@ -130,7 +128,7 @@ sidebar: auto
130
128
131
129
为某个特定的 Vuex 实例打开或关闭 devtools。对于传入 ` false ` 的实例来说 Vuex store 不会订阅到 devtools 插件。对于一个页面中有多个 store 的情况非常有用。
132
130
133
- ` ` ` js
131
+ ` ` ` js
134
132
{
135
133
devtools: false
136
134
}
@@ -154,15 +152,15 @@ sidebar: auto
154
152
155
153
### commit
156
154
157
- - ` commit (type: string, payload?: any, options?: Object )`
158
- - ` commit (mutation: Object , options?: Object )`
155
+ - ` commit (type: string, payload?: any, options?: Object )`
156
+ - ` commit (mutation: Object , options?: Object )`
159
157
160
158
提交 mutation。` options` 里可以有 ` root: true ` ,它允许在[命名空间模块](../guide/modules.md#命名空间)里提交根的 mutation。[详细介绍](../guide/mutations.md)
161
159
162
160
### dispatch
163
161
164
- - ` dispatch (type: string, payload?: any, options?: Object ): Promise < any> `
165
- - ` dispatch (action: Object , options?: Object ): Promise < any> `
162
+ - ` dispatch (type: string, payload?: any, options?: Object ): Promise < any> `
163
+ - ` dispatch (action: Object , options?: Object ): Promise < any> `
166
164
167
165
分发 action。` options` 里可以有 ` root: true ` ,它允许在[命名空间模块](../guide/modules.md#命名空间)里分发根的 action。返回一个解析所有被触发的 action 处理器的 Promise。[详细介绍](../guide/actions.md)
168
166
@@ -174,58 +172,66 @@ sidebar: auto
174
172
175
173
### watch
176
174
177
- - ` watch (fn: Function , callback: Function , options?: Object ): Function `
175
+ - ` watch (fn: Function , callback: Function , options?: Object ): Function `
178
176
179
177
响应式地侦听 ` fn` 的返回值,当值改变时调用回调函数。` fn` 接收 store 的 state 作为第一个参数,其 getter 作为第二个参数。最后接收一个可选的对象参数表示 Vue 的 [` vm .$watch ` ](https://cn.vuejs.org/v2/api/#vm-watch) 方法的参数。
180
178
181
179
要停止侦听,调用此方法返回的函数即可停止侦听。
182
180
183
181
### subscribe
184
182
185
- - ` subscribe (handler: Function , options?: Object ): Function `
183
+ - ` subscribe (handler: Function , options?: Object ): Function `
186
184
187
185
订阅 store 的 mutation。` handler` 会在每个 mutation 完成后调用,接收 mutation 和经过 mutation 后的状态作为参数:
188
186
189
- ` ` ` js
190
- store .subscribe ((mutation , state ) => {
187
+ ` ` ` js
188
+ const unsubscribe = store .subscribe ((mutation , state ) => {
191
189
console .log (mutation .type )
192
190
console .log (mutation .payload )
193
191
})
192
+
193
+ // 你可以调用 unsubscribe 来停止订阅。
194
+ unsubscribe ()
194
195
` ` `
195
196
196
197
默认情况下,新的处理函数会被添加到其链的尾端,因此它会在其它之前已经被添加了的处理函数之后执行。这一行为可以通过向 ` options` 添加 ` prepend: true ` 来覆写,即把处理函数添加到其链的最开始。
197
198
198
- ` ` ` js
199
+ ` ` ` js
199
200
store .subscribe (handler, { prepend: true })
200
201
` ` `
201
- 要停止订阅,调用此方法返回的函数即可停止订阅。
202
+
203
+ ` subscribe` 方法将返回一个 ` unsubscribe` 函数,当不再需要订阅时应该调用该函数。例如,你可能会订阅一个 Vuex 模块,当你取消注册该模块时取消订阅。或者你可能从一个 Vue 组件内部调用 ` subscribe` ,然后不久就会销毁该组件。在这些情况下,你应该记得手动取消订阅。
202
204
203
205
通常用于插件。[详细介绍](../guide/plugins.md)
204
206
205
207
### subscribeAction
206
208
207
- - ` subscribeAction (handler: Function , options?: Object ): Function `
209
+ - ` subscribeAction (handler: Function , options?: Object ): Function `
208
210
209
- 订阅 store 的 action。` handler` 会在每个 action 分发的时候调用并接收 action 描述和当前的 store 的 state 这两个参数:
211
+ 订阅 store 的 action。` handler` 会在每个 action 分发的时候调用并接收 action 描述和当前的 store 的 state 这两个参数。
212
+ ` subscribe` 方法将返回一个 ` unsubscribe` 函数,当不再需要订阅时,应调用该函数。例如,当取消注册一个 Vuex 模块或销毁一个 Vue 组件之前。
210
213
211
- ` ` ` js
212
- store .subscribeAction ((action , state ) => {
214
+ ` ` ` js
215
+ const unsubscribe = store .subscribeAction ((action , state ) => {
213
216
console .log (action .type )
214
217
console .log (action .payload )
215
218
})
219
+
220
+ // 你可以调用 unsubscribe 来停止订阅。
221
+ unsubscribe ()
216
222
` ` `
217
223
218
224
默认情况下,新的处理函数会被添加到其链的尾端,因此它会在其它之前已经被添加了的处理函数之后执行。这一行为可以通过向 ` options` 添加 ` prepend: true ` 来覆写,即把处理函数添加到其链的最开始。
219
225
220
- ` ` ` js
226
+ ` ` ` js
221
227
store .subscribeAction (handler, { prepend: true })
222
228
` ` `
223
229
224
- 要停止订阅,调用此方法返回的函数即可停止订阅 。
230
+ ` subscribeAction ` 方法将返回一个 ` unsubscribe ` 函数,当不再需要订阅时,应该调用该函数。例如,你可能会订阅一个 Vuex 模块,并在取消注册该模块时取消订阅。或者你可能从 Vue 组件内部调用 ` subscribeAction ` ,然后不久就会销毁该组件。在这些情况下,你应该记得手动取消订阅 。
225
231
226
232
` subscribeAction` 也可以指定订阅处理函数的被调用时机应该在一个 action 分发*之前*还是*之后* (默认行为是*之前*):
227
233
228
- ` ` ` js
234
+ ` ` ` js
229
235
store .subscribeAction ({
230
236
before : (action , state ) => {
231
237
console .log (` before action ${ action .type } ` )
@@ -238,7 +244,7 @@ sidebar: auto
238
244
239
245
` subscribeAction` 也可以指定一个 ` error` 处理函数以捕获分发 action 的时候被抛出的错误。该函数会从第三个参数接收到一个 ` error` 对象。
240
246
241
- ` ` ` js
247
+ ` ` ` js
242
248
store .subscribeAction ({
243
249
error : (action , state , error ) => {
244
250
console .log (` error action ${ action .type } ` )
@@ -251,15 +257,15 @@ sidebar: auto
251
257
252
258
### registerModule
253
259
254
- - ` registerModule (path: string | Array < string> , module: Module, options?: Object )`
260
+ - ` registerModule (path: string | Array < string> , module: Module, options?: Object )`
255
261
256
262
注册一个动态模块。[详细介绍](../guide/modules.md#模块动态注册)
257
263
258
264
` options` 可以包含 ` preserveState: true ` 以允许保留之前的 state。用于服务端渲染。
259
265
260
266
### unregisterModule
261
267
262
- - ` unregisterModule (path: string | Array < string> )`
268
+ - ` unregisterModule (path: string | Array < string> )`
263
269
264
270
卸载一个动态模块。[详细介绍](../guide/modules.md#模块动态注册)
265
271
@@ -271,15 +277,15 @@ sidebar: auto
271
277
272
278
### hotUpdate
273
279
274
- - ` hotUpdate (newOptions: Object )`
280
+ - ` hotUpdate (newOptions: Object )`
275
281
276
282
热替换新的 action 和 mutation。[详细介绍](../guide/hot-reload.md)
277
283
278
284
## 组件绑定的辅助函数
279
285
280
286
### mapState
281
287
282
- - ` mapState (namespace?: string, map: Array < string> | Object < string | function >): Object`
288
+ - ` mapState (namespace?: string, map: Array < string> | Object < string | function >): Object`
283
289
284
290
为组件创建计算属性以返回 Vuex store 中的状态。[详细介绍](../guide/state.md#mapstate-辅助函数)
285
291
@@ -289,15 +295,15 @@ sidebar: auto
289
295
290
296
### mapGetters
291
297
292
- - `mapGetters(namespace?: string, map: Array<string> | Object<string>): Object`
298
+ - `mapGetters(namespace?: string, map: Array<string> | Object<string>): Object`
293
299
294
300
为组件创建计算属性以返回 getter 的返回值。[详细介绍](../guide/getters.md#mapgetters-辅助函数)
295
301
296
302
第一个参数是可选的,可以是一个命名空间字符串。[详细介绍](../guide/modules.md#带命名空间的绑定函数)
297
303
298
304
### mapActions
299
305
300
- - `mapActions(namespace?: string, map: Array<string> | Object<string | function>): Object`
306
+ - `mapActions(namespace?: string, map: Array<string> | Object<string | function>): Object`
301
307
302
308
创建组件方法分发 action。[详细介绍](../guide/actions.md#在组件中分发-action)
303
309
@@ -307,7 +313,7 @@ sidebar: auto
307
313
308
314
### mapMutations
309
315
310
- - `mapMutations(namespace?: string, map: Array<string> | Object<string | function>): Object`
316
+ - `mapMutations(namespace?: string, map: Array<string> | Object<string | function>): Object`
311
317
312
318
创建组件方法提交 mutation。[详细介绍](../guide/mutations.md#在组件中提交-mutation)
313
319
@@ -378,7 +384,7 @@ sidebar: auto
378
384
最后,将 key 传递给 ` useStore` 方法以获取指定类型的 store 实例。
379
385
380
386
` ` ` ts
381
- // vue 组件
387
+ // 在 vue 组件内
382
388
import { useStore } from ' vuex'
383
389
import { key } from ' ./store'
384
390
0 commit comments