Skip to content

Commit eaf53da

Browse files
jingsamyyx990803
jingsam
authored andcommitted
Fixed symlink problem on Windows
add zh-cn finish intro.md finish mutations finished all translations
1 parent 8609536 commit eaf53da

23 files changed

+1622
-2
lines changed

docs/LANGS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
* [2.0 - English](en/)
2+
* [2.0 - 简体中文](zh-cn/)
23
* [2.0 - Français](fr/)
34
* [1.0 Docs](old/)

docs/en/README.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/en/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Vuex
2+
3+
> Note: This is docs for vuex@2.x.
4+
5+
- [Looking for 1.0 Docs?](https://github.com/vuejs/vuex/tree/1.0/docs)
6+
- [Release Notes](https://github.com/vuejs/vuex/releases)
7+
- [Installation](installation.md)
8+
- [What is Vuex?](intro.md)
9+
- [Getting Started](getting-started.md)
10+
- Core Concepts
11+
- [State](state.md)
12+
- [Getters](getters.md)
13+
- [Mutations](mutations.md)
14+
- [Actions](actions.md)
15+
- [Modules](modules.md)
16+
- [Application Structure](structure.md)
17+
- [Plugins](plugins.md)
18+
- [Strict Mode](strict.md)
19+
- [Form Handling](forms.md)
20+
- [Testing](testing.md)
21+
- [Hot Reloading](hot-reload.md)
22+
- [API Reference](api.md)

docs/en/book.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/en/book.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"gitbook": "2.x.x",
3+
"plugins": ["edit-link", "prism", "-highlight", "github"],
4+
"pluginsConfig": {
5+
"edit-link": {
6+
"base": "https://github.com/vuejs/vuex/tree/dev/docs",
7+
"label": "Edit This Page"
8+
},
9+
"github": {
10+
"url": "https://github.com/vuejs/vuex/"
11+
}
12+
},
13+
"links": {
14+
"sharing": {
15+
"facebook": false,
16+
"twitter": false
17+
}
18+
}
19+
}

docs/zh-cn/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Vuex
2+
3+
> 注意:本文档针对的是 vuex@2.x
4+
5+
- [寻找 1.0 文档?](https://github.com/vuejs/vuex/tree/1.0/docs)
6+
- [更新记录](https://github.com/vuejs/vuex/releases)
7+
- [安装](installation.md)
8+
- [Vuex 是什么?](intro.md)
9+
- [开始](getting-started.md)
10+
- 核心概念
11+
- [State](state.md)
12+
- [Getters](getters.md)
13+
- [Mutations](mutations.md)
14+
- [Actions](actions.md)
15+
- [Modules](modules.md)
16+
- [项目结构](structure.md)
17+
- [插件](plugins.md)
18+
- [严格模式](strict.md)
19+
- [表单处理](forms.md)
20+
- [测试](testing.md)
21+
- [热重载](hot-reload.md)
22+
- [API 文档](api.md)

docs/zh-cn/SUMMARY.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Vuex
2+
3+
> 注意:本文档针对的是 vuex@2.x
4+
5+
- [寻找 1.0 文档?](https://github.com/vuejs/vuex/tree/1.0/docs)
6+
- [更新记录](https://github.com/vuejs/vuex/releases)
7+
- [安装](installation.md)
8+
- [Vuex 是什么?](intro.md)
9+
- [开始](getting-started.md)
10+
- 核心概念
11+
- [State](state.md)
12+
- [Getters](getters.md)
13+
- [Mutations](mutations.md)
14+
- [Actions](actions.md)
15+
- [Modules](modules.md)
16+
- [项目结构](structure.md)
17+
- [插件](plugins.md)
18+
- [严格模式](strict.md)
19+
- [表单处理](forms.md)
20+
- [测试](testing.md)
21+
- [热重载](hot-reload.md)
22+
- [API 文档](api.md)

docs/zh-cn/actions.md

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# Actions
2+
3+
Action 类似于 mutation,不同在于:
4+
5+
- Action 提交的是 mutation,而不是直接变更状态。
6+
- Action 可以包含任意异步操作。
7+
8+
让我们来注册一个简单的 action:
9+
10+
``` js
11+
const store = new Vuex.Store({
12+
state: {
13+
count: 0
14+
},
15+
mutations: {
16+
increment (state) {
17+
state.count++
18+
}
19+
},
20+
actions: {
21+
increment (context) {
22+
context.commit('increment')
23+
}
24+
}
25+
})
26+
```
27+
28+
Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象,因此你可以调用 `context.commit` 提交一个 mutation,或者通过 `context.state``context.getters` 来获取 state 和 getters。当我们在之后介绍到 [Modules](modules.md) 时,你就知道 context 对象为什么不是 store 实例本身了。
29+
30+
实践中,我们会经常会用到 ES2015 的 [参数解构](https://github.com/lukehoban/es6features#destructuring) 来简化代码(特别是我们需要调用 `commit` 很多次的时候):
31+
32+
``` js
33+
actions: {
34+
increment ({ commit }) {
35+
commit('increment')
36+
}
37+
}
38+
```
39+
40+
### 分发 Action
41+
42+
Action 通过 `store.dispatch` 方法触发:
43+
44+
``` js
45+
store.dispatch('increment')
46+
```
47+
48+
乍一眼看上去感觉多此一举,我们直接分发 mutation 岂不更方便?实际上并非如此,还记得 **mutation 必须同步执行**这个限制么?Action 就不受约束!我们可以在 action 内部执行**异步**操作:
49+
50+
``` js
51+
actions: {
52+
incrementAsync ({ commit }) {
53+
setTimeout(() => {
54+
commit('increment')
55+
}, 1000)
56+
}
57+
}
58+
```
59+
60+
Actions 支持同样的载荷方式和对象方式进行分发:
61+
62+
``` js
63+
// 以载荷形式分发
64+
store.dispatch('incrementAsync', {
65+
amount: 10
66+
})
67+
68+
// 以对象形式分发
69+
store.dispatch({
70+
type: 'incrementAsync',
71+
amount: 10
72+
})
73+
```
74+
75+
来看一个更加实际的购物车示例,涉及到**调用异步 API****分发多重 mutations**
76+
77+
``` js
78+
actions: {
79+
checkout ({ commit, state }, payload) {
80+
// 把当前购物车的物品备份起来
81+
const savedCartItems = [...state.cart.added]
82+
// 发出结账请求,然后乐观地清空购物车
83+
commit(types.CHECKOUT_REQUEST)
84+
// 购物 API 接受一个成功回调和一个失败回调
85+
shop.buyProducts(
86+
products,
87+
// 成功操作
88+
() => commit(types.CHECKOUT_SUCCESS),
89+
// 失败操作
90+
() => commit(types.CHECKOUT_FAILURE, savedCartItems)
91+
)
92+
}
93+
}
94+
```
95+
96+
注意我们正在进行一系列的异步操作,并且通过提交 mutation 来记录 action 产生的副作用(即状态变更)。
97+
98+
### 在组件中分发 Action
99+
100+
你在组件中使用 `this.$store.dispatch('xxx')` 分发 action,或者使用 `mapActions` 辅助函数将组件的 methods 映射为 `store.dispatch` 调用(需要先在根节点注入 `store`):
101+
102+
``` js
103+
import { mapActions } from 'vuex'
104+
105+
export default {
106+
// ...
107+
methods: {
108+
...mapActions([
109+
'increment' // 映射 this.increment() 为 this.$store.dispatch('increment')
110+
]),
111+
...mapActions({
112+
add: 'increment' // 映射 this.add() 为 this.$store.dispatch('increment')
113+
})
114+
}
115+
}
116+
```
117+
118+
### 组合 Actions
119+
120+
Action 通常是异步的,那么如何知道 action 什么时候结束呢?更重要的是,我们如何才能组合多个 action,以处理更加复杂的异步流程?
121+
122+
第一件事你需要清楚的是 `store.dispatch` 的返回的是被触发的 action 函数的返回值,因此你可以在 action 中返回 Promise:
123+
124+
``` js
125+
actions: {
126+
actionA ({ commit }) {
127+
return new Promise((resolve, reject) => {
128+
setTimeout(() => {
129+
commit('someMutation')
130+
resolve()
131+
}, 1000)
132+
})
133+
}
134+
}
135+
```
136+
137+
现在你可以:
138+
139+
``` js
140+
store.dispatch('actionA').then(() => {
141+
// ...
142+
})
143+
```
144+
145+
在另外一个 action 中也可以:
146+
147+
``` js
148+
actions: {
149+
// ...
150+
actionB ({ dispatch, commit }) {
151+
return dispatch('actionA').then(() => {
152+
commit('someOtherMutation')
153+
})
154+
}
155+
}
156+
```
157+
158+
最后,如果我们利用 [async / await](https://tc39.github.io/ecmascript-asyncawait/) 这个 JavaScript 即将到来的新特性,我们可以像这样组合 action:
159+
160+
``` js
161+
// 假设 getData() 和 getOtherData() 返回的是 Promise
162+
163+
actions: {
164+
async actionA ({ commit }) {
165+
commit('gotData', await getData())
166+
},
167+
async actionB ({ dispatch, commit }) {
168+
await dispatch('actionA') // 等待 actionA 完成
169+
commit('gotOtherData', await getOtherData())
170+
}
171+
}
172+
```
173+
174+
> 一个 `store.dispatch` 在不同模块中可以触发多个 action 函数。在这种情况下,只有当所有触发函数完成后,返回的 Promise 才会执行。

0 commit comments

Comments
 (0)