Skip to content

Commit 01f87f0

Browse files
docs(zh): update zh docs from en docs (#2150)
* docs(zh): update zh docs from en docs * Update docs/zh/api/index.md Co-authored-by: Jinjiang <zhaojinjiang@me.com> * docs(zh): apply suggestions * docs: apply suggestions Co-authored-by: Jinjiang <zhaojinjiang@me.com>
1 parent 75d20ec commit 01f87f0

9 files changed

+67
-48
lines changed

docs/zh/api/index.md

+41-35
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ sidebar: auto
4040

4141
- 类型: `{ [type: string]: Function }`
4242

43-
在 store 上注册 action。处理函数总是接受 `context` 作为第一个参数,`payload` 作为第二个参数(可选)。
44-
45-
`context` 对象包含以下属性:
43+
在 store 上注册 action。处理函数总是接受 `context` 作为第一个参数,`context` 对象包含以下属性:
4644

4745
``` js
4846
{
@@ -67,15 +65,15 @@ sidebar: auto
6765

6866
```
6967
state, // 如果在模块中定义则为模块的局部状态
70-
getters, // 等同于 store.getters
68+
getters // 等同于 store.getters
7169
```
7270

7371
当定义在一个模块里时会特别一些:
7472

7573
```
7674
state, // 如果在模块中定义则为模块的局部状态
77-
getters, // 等同于 store.getters
78-
rootState // 等同于 store.state
75+
getters, // 当前模块的局部 getters
76+
rootState, // 全局 state
7977
rootGetters // 所有 getters
8078
```
8179

@@ -89,12 +87,12 @@ sidebar: auto
8987

9088
包含了子模块的对象,会被合并到 store,大概长这样:
9189

92-
``` js
90+
```js
9391
{
9492
key: {
9593
state,
9694
namespaced?,
97-
mutations,
95+
mutations?,
9896
actions?,
9997
getters?,
10098
modules?
@@ -130,7 +128,7 @@ sidebar: auto
130128
131129
为某个特定的 Vuex 实例打开或关闭 devtools。对于传入 `false` 的实例来说 Vuex store 不会订阅到 devtools 插件。对于一个页面中有多个 store 的情况非常有用。
132130
133-
``` js
131+
```js
134132
{
135133
devtools: false
136134
}
@@ -154,15 +152,15 @@ sidebar: auto
154152
155153
### commit
156154
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)`
159157
160158
提交 mutation。`options` 里可以有 `root: true`,它允许在[命名空间模块](../guide/modules.md#命名空间)里提交根的 mutation。[详细介绍](../guide/mutations.md)
161159
162160
### dispatch
163161
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>`
166164
167165
分发 action。`options` 里可以有 `root: true`,它允许在[命名空间模块](../guide/modules.md#命名空间)里分发根的 action。返回一个解析所有被触发的 action 处理器的 Promise。[详细介绍](../guide/actions.md)
168166
@@ -174,58 +172,66 @@ sidebar: auto
174172
175173
### watch
176174
177-
- `watch(fn: Function, callback: Function, options?: Object): Function`
175+
- `watch(fn: Function, callback: Function, options?: Object): Function`
178176
179177
 响应式地侦听 `fn` 的返回值,当值改变时调用回调函数。`fn` 接收 store 的 state 作为第一个参数,其 getter 作为第二个参数。最后接收一个可选的对象参数表示 Vue 的 [`vm.$watch`](https://cn.vuejs.org/v2/api/#vm-watch) 方法的参数。
180178
181179
 要停止侦听,调用此方法返回的函数即可停止侦听。
182180
183181
### subscribe
184182
185-
- `subscribe(handler: Function, options?: Object): Function`
183+
- `subscribe(handler: Function, options?: Object): Function`
186184
187185
订阅 store 的 mutation。`handler` 会在每个 mutation 完成后调用,接收 mutation 和经过 mutation 后的状态作为参数:
188186
189-
``` js
190-
store.subscribe((mutation, state) => {
187+
```js
188+
const unsubscribe = store.subscribe((mutation, state) => {
191189
console.log(mutation.type)
192190
console.log(mutation.payload)
193191
})
192+
193+
// 你可以调用 unsubscribe 来停止订阅。
194+
unsubscribe()
194195
```
195196
196197
默认情况下,新的处理函数会被添加到其链的尾端,因此它会在其它之前已经被添加了的处理函数之后执行。这一行为可以通过向 `options` 添加 `prepend: true` 来覆写,即把处理函数添加到其链的最开始。
197198
198-
``` js
199+
```js
199200
store.subscribe(handler, { prepend: true })
200201
```
201-
 要停止订阅,调用此方法返回的函数即可停止订阅。
202+
203+
`subscribe` 方法将返回一个 `unsubscribe` 函数,当不再需要订阅时应该调用该函数。例如,你可能会订阅一个 Vuex 模块,当你取消注册该模块时取消订阅。或者你可能从一个 Vue 组件内部调用 `subscribe`,然后不久就会销毁该组件。在这些情况下,你应该记得手动取消订阅。
202204
203205
通常用于插件。[详细介绍](../guide/plugins.md)
204206
205207
### subscribeAction
206208
207-
- `subscribeAction(handler: Function, options?: Object): Function`
209+
- `subscribeAction(handler: Function, options?: Object): Function`
208210
209-
订阅 store 的 action。`handler` 会在每个 action 分发的时候调用并接收 action 描述和当前的 store 的 state 这两个参数:
211+
订阅 store 的 action。`handler` 会在每个 action 分发的时候调用并接收 action 描述和当前的 store 的 state 这两个参数。
212+
`subscribe` 方法将返回一个 `unsubscribe` 函数,当不再需要订阅时,应调用该函数。例如,当取消注册一个 Vuex 模块或销毁一个 Vue 组件之前。
210213
211-
``` js
212-
store.subscribeAction((action, state) => {
214+
```js
215+
const unsubscribe = store.subscribeAction((action, state) => {
213216
console.log(action.type)
214217
console.log(action.payload)
215218
})
219+
220+
// 你可以调用 unsubscribe 来停止订阅。
221+
unsubscribe()
216222
```
217223
218224
默认情况下,新的处理函数会被添加到其链的尾端,因此它会在其它之前已经被添加了的处理函数之后执行。这一行为可以通过向 `options` 添加 `prepend: true` 来覆写,即把处理函数添加到其链的最开始。
219225
220-
``` js
226+
```js
221227
store.subscribeAction(handler, { prepend: true })
222228
```
223229
224-
要停止订阅,调用此方法返回的函数即可停止订阅
230+
`subscribeAction` 方法将返回一个 `unsubscribe` 函数,当不再需要订阅时,应该调用该函数。例如,你可能会订阅一个 Vuex 模块,并在取消注册该模块时取消订阅。或者你可能从 Vue 组件内部调用`subscribeAction`,然后不久就会销毁该组件。在这些情况下,你应该记得手动取消订阅
225231
226232
`subscribeAction` 也可以指定订阅处理函数的被调用时机应该在一个 action 分发*之前*还是*之后* (默认行为是*之前*):
227233
228-
``` js
234+
```js
229235
store.subscribeAction({
230236
before: (action, state) => {
231237
console.log(`before action ${action.type}`)
@@ -238,7 +244,7 @@ sidebar: auto
238244
239245
`subscribeAction` 也可以指定一个 `error` 处理函数以捕获分发 action 的时候被抛出的错误。该函数会从第三个参数接收到一个 `error` 对象。
240246
241-
``` js
247+
```js
242248
store.subscribeAction({
243249
error: (action, state, error) => {
244250
console.log(`error action ${action.type}`)
@@ -251,15 +257,15 @@ sidebar: auto
251257
252258
### registerModule
253259
254-
- `registerModule(path: string | Array<string>, module: Module, options?: Object)`
260+
- `registerModule(path: string | Array<string>, module: Module, options?: Object)`
255261
256262
注册一个动态模块。[详细介绍](../guide/modules.md#模块动态注册)
257263
258264
`options` 可以包含 `preserveState: true` 以允许保留之前的 state。用于服务端渲染。
259265
260266
### unregisterModule
261267
262-
- `unregisterModule(path: string | Array<string>)`
268+
- `unregisterModule(path: string | Array<string>)`
263269
264270
卸载一个动态模块。[详细介绍](../guide/modules.md#模块动态注册)
265271
@@ -271,15 +277,15 @@ sidebar: auto
271277
272278
### hotUpdate
273279
274-
- `hotUpdate(newOptions: Object)`
280+
- `hotUpdate(newOptions: Object)`
275281
276282
热替换新的 action 和 mutation。[详细介绍](../guide/hot-reload.md)
277283
278284
## 组件绑定的辅助函数
279285
280286
### mapState
281287
282-
- `mapState(namespace?: string, map: Array<string> | Object<string | function>): Object`
288+
- `mapState(namespace?: string, map: Array<string> | Object<string | function>): Object`
283289

284290
为组件创建计算属性以返回 Vuex store 中的状态。[详细介绍](../guide/state.md#mapstate-辅助函数)
285291

@@ -289,15 +295,15 @@ sidebar: auto
289295

290296
### mapGetters
291297

292-
- `mapGetters(namespace?: string, map: Array<string> | Object<string>): Object`
298+
- `mapGetters(namespace?: string, map: Array<string> | Object<string>): Object`
293299

294300
为组件创建计算属性以返回 getter 的返回值。[详细介绍](../guide/getters.md#mapgetters-辅助函数)
295301

296302
第一个参数是可选的,可以是一个命名空间字符串。[详细介绍](../guide/modules.md#带命名空间的绑定函数)
297303

298304
### mapActions
299305

300-
- `mapActions(namespace?: string, map: Array<string> | Object<string | function>): Object`
306+
- `mapActions(namespace?: string, map: Array<string> | Object<string | function>): Object`
301307

302308
创建组件方法分发 action。[详细介绍](../guide/actions.md#在组件中分发-action)
303309

@@ -307,7 +313,7 @@ sidebar: auto
307313

308314
### mapMutations
309315

310-
- `mapMutations(namespace?: string, map: Array<string> | Object<string | function>): Object`
316+
- `mapMutations(namespace?: string, map: Array<string> | Object<string | function>): Object`
311317

312318
创建组件方法提交 mutation。[详细介绍](../guide/mutations.md#在组件中提交-mutation)
313319

@@ -378,7 +384,7 @@ sidebar: auto
378384
最后,将 key 传递给 `useStore` 方法以获取指定类型的 store 实例。
379385
380386
```ts
381-
// vue 组件
387+
// vue 组件内
382388
import { useStore } from 'vuex'
383389
import { key } from './store'
384390

docs/zh/guide/actions.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ actions: {
8181
checkout ({ commit, state }, products) {
8282
// 把当前购物车的物品备份起来
8383
const savedCartItems = [...state.cart.added]
84-
// 发出结账请求,然后乐观地清空购物车
84+
// 发出结账请求
85+
// 然后乐观地清空购物车
8586
commit(types.CHECKOUT_REQUEST)
8687
// 购物 API 接受一个成功回调和一个失败回调
8788
shop.buyProducts(

docs/zh/guide/forms.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
``` html
1616
<input :value="message" @input="updateMessage">
1717
```
18+
1819
``` js
1920
// ...
2021
computed: {
@@ -47,6 +48,7 @@ mutations: {
4748
``` html
4849
<input v-model="message">
4950
```
51+
5052
``` js
5153
// ...
5254
computed: {

docs/zh/guide/getters.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ computed: {
1717
Vuex 允许我们在 store 中定义“getter”(可以认为是 store 的计算属性)。
1818

1919
::: warning 注意
20-
从 Vue 3.0 开始,getter 的结果不再像计算属性一样会被缓存起来。这是一个已知的问题,将会在 3.2 版本中修复。详情请看 [PR #1878](https://github.com/vuejs/vuex/pull/1883)
20+
从 Vue 3.0 开始,getter 的结果不再像计算属性一样会被缓存起来。这是一个已知的问题,将会在 3.1 版本中修复。详情请看 [PR #1878](https://github.com/vuejs/vuex/pull/1883)
2121
:::
2222

2323
Getter 接受 state 作为其第一个参数:
@@ -31,7 +31,7 @@ const store = createStore({
3131
]
3232
},
3333
getters: {
34-
doneTodos: (state) => {
34+
doneTodos (state) {
3535
return state.todos.filter(todo => todo.done)
3636
}
3737
}

docs/zh/guide/modules.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ const moduleA = {
4646
state.count++
4747
}
4848
},
49-
5049
getters: {
5150
doubleCount (state) {
5251
return state.count * 2
@@ -151,6 +150,7 @@ modules: {
151150
someGetter (state, getters, rootState, rootGetters) {
152151
getters.someOtherGetter // -> 'foo/someOtherGetter'
153152
rootGetters.someOtherGetter // -> 'someOtherGetter'
153+
rootGetters['bar/someOtherGetter'] // -> 'bar/someOtherGetter'
154154
},
155155
someOtherGetter: state => { ... }
156156
},
@@ -161,6 +161,7 @@ modules: {
161161
someAction ({ dispatch, commit, getters, rootGetters }) {
162162
getters.someGetter // -> 'foo/someGetter'
163163
rootGetters.someGetter // -> 'someGetter'
164+
rootGetters['bar/someGetter'] // -> 'bar/someGetter'
164165

165166
dispatch('someOtherAction') // -> 'foo/someOtherAction'
166167
dispatch('someOtherAction', null, { root: true }) // -> 'someOtherAction'
@@ -209,7 +210,11 @@ computed: {
209210
...mapState({
210211
a: state => state.some.nested.module.a,
211212
b: state => state.some.nested.module.b
212-
})
213+
}),
214+
...mapGetters([
215+
'some/nested/module/someGetter', // -> this['some/nested/module/someGetter']
216+
'some/nested/module/someOtherGetter', // -> this['some/nested/module/someOtherGetter']
217+
])
213218
},
214219
methods: {
215220
...mapActions([
@@ -226,7 +231,11 @@ computed: {
226231
...mapState('some/nested/module', {
227232
a: state => state.a,
228233
b: state => state.b
229-
})
234+
}),
235+
...mapGetters('some/nested/module', [
236+
'someGetter', // -> this.someGetter
237+
'someOtherGetter', // -> this.someOtherGetter
238+
])
230239
},
231240
methods: {
232241
...mapActions('some/nested/module', [
@@ -290,6 +299,7 @@ const store = createStore({ /* 选项 */ })
290299
store.registerModule('myModule', {
291300
// ...
292301
})
302+
293303
// 注册嵌套模块 `nested/myModule`
294304
store.registerModule(['nested', 'myModule'], {
295305
// ...

docs/zh/guide/mutations.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ mutations: {
3636
}
3737
}
3838
```
39+
3940
``` js
4041
store.commit('increment', 10)
4142
```
@@ -95,7 +96,8 @@ import { SOME_MUTATION } from './mutation-types'
9596
const store = createStore({
9697
state: { ... },
9798
mutations: {
98-
// 我们可以使用 ES2015 风格的计算属性命名功能来使用一个常量作为函数名
99+
// 我们可以使用 ES2015 风格的计算属性命名功能
100+
// 来使用一个常量作为函数名
99101
[SOME_MUTATION] (state) {
100102
// 修改 state
101103
}

docs/zh/guide/plugins.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const store = createStore({
2727

2828
在插件中不允许直接修改状态——类似于组件,只能通过提交 mutation 来触发变化。
2929

30-
通过提交 mutation,插件可以用来同步数据源到 store。例如,同步 websocket 数据源到 store(下面是个大概例子,实际上 `createPlugin` 方法可以有更多选项来完成复杂任务):
30+
通过提交 mutation,插件可以用来同步数据源到 store。例如,同步 websocket 数据源到 store(下面是个大概例子,实际上 `createWebSocketPlugin` 方法可以有更多选项来完成复杂任务):
3131

3232
``` js
3333
export default function createWebSocketPlugin (socket) {
@@ -90,9 +90,9 @@ const store = createStore({
9090
Vuex 自带一个日志插件用于一般的调试:
9191

9292
``` js
93-
import createLogger from 'vuex/dist/logger'
93+
import { createLogger } from 'vuex'
9494

95-
const store = new Vuex.Store({
95+
const store = createStore({
9696
plugins: [createLogger()]
9797
})
9898
```

docs/zh/guide/testing.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ const testAction = (action, args, state, expectedMutations, done) => {
119119

120120
describe('actions', () => {
121121
it('getAllProducts', done => {
122-
testAction(actions.getAllProducts, [], {}, [
122+
testAction(actions.getAllProducts, null, {}, [
123123
{ type: 'REQUEST_PRODUCTS' },
124124
{ type: 'RECEIVE_PRODUCTS', payload: { /* mocked response */ } }
125125
], done)

0 commit comments

Comments
 (0)