Skip to content

Commit d4a47d3

Browse files
authored
Merge pull request #165 from dear-lizhihua/2.0-cn
/vuex/mutations.md
2 parents ddd750e + be4f255 commit d4a47d3

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

src/vuex/mutations.md

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ type: vuex
44
order: 6
55
---
66

7-
The only way to actually change state in a Vuex store is by committing a mutation. Vuex mutations are very similar to events: each mutation has a string **type** and a **handler**. The handler function is where we perform actual state modifications, and it will receive the state as the first argument:
7+
Vuex store 中,实际改变 状态(state) 的唯一方式是通过 提交(commit) 一个 mutation Vuex 的 mutation 和事件系统非常相似:每个 mutation 都有一个字符串 **类型(type)** 和 一个 **回调函数(handler)**。回调函数是我们执行实际修改状态的地方,它将接收 状态(state) 作为第一个参数。
88

99
``` js
1010
const store = new Vuex.Store({
@@ -13,22 +13,22 @@ const store = new Vuex.Store({
1313
},
1414
mutations: {
1515
increment (state) {
16-
// mutate state
16+
// 改变 state
1717
state.count++
1818
}
1919
}
2020
})
2121
```
2222

23-
You cannot directly call a mutation handler. The options here is more like event registration: "When a mutation with type `increment` is triggered, call this handler." To invoke a mutation handler, you need to call **store.commit** with its type:
23+
你不能直接调用 mutation 的回调函数。选项 mutations 在这里更像是注册事件:“当触发类型为 `increment` mutation 时,执行其回调函数。”所以你需要调用该类型的 **store.commit** 才能执行 mutation 的回调函数。
2424

2525
``` js
2626
store.commit('increment')
2727
```
2828

29-
### Commit with Payload
29+
### Commit 传入 Payload
3030

31-
You can pass an additional argument to `store.commit`, which is called the **payload** for the mutation:
31+
`store.commit` 传递一个额外的参数,这个参数被称为 **payload**
3232

3333
``` js
3434
// ...
@@ -42,7 +42,7 @@ mutations: {
4242
store.commit('increment', 10)
4343
```
4444

45-
In most cases, the payload should be an object so that it can contain multiple fields, and the recorded mutation will also be more descriptive:
45+
多数情况下,payload 应该是一个对象,以便它可以包含多个字段,这样 mutation 记录中有了 payload 字段名,可描述性会变得更好。
4646

4747
``` js
4848
// ...
@@ -58,9 +58,9 @@ store.commit('increment', {
5858
})
5959
```
6060

61-
### Object-Style Commit
61+
### 对象风格的 Commit
6262

63-
An alternative way to commit a mutation is by directly using an object that has a `type` property:
63+
提交 mutation 的另一种替代方式,是直接使用具有 `type` 属性的对象:
6464

6565
``` js
6666
store.commit({
@@ -69,7 +69,7 @@ store.commit({
6969
})
7070
```
7171

72-
When using object-style commit, the entire object will be passed as the payload to mutation handlers, so the handler remains the same:
72+
当使用对象风格的 commit,整个对象都会被作为 payload 参数传入到对应类型的 mutation 的回调函数中,不过回调函数还保持不变:
7373

7474
``` js
7575
mutations: {
@@ -79,43 +79,43 @@ mutations: {
7979
}
8080
```
8181

82-
### Silent Commit
82+
### 静默的 Commit
8383

84-
> Note: This is a feature that will likely be deprecated once we implement mutation filtering in the devtools.
84+
> 注意:一旦我们实现了 devtools 中过滤 mutation,此特性可能会被弃用。
8585
86-
By default, every committed mutation is sent to plugins (e.g. the devtools). However in some scenarios you may not want the plugins to record every state change. Multiple commits to the store in a short period or polled do not always need to be tracked. In such cases you can pass a third argument to `store.commit` to "silence" that specific mutation from plugins:
86+
默认情况下,每个提交过的 mutation 都会被发送到插件(如 devtools)。然而在某些情况下,你可能不希望插件去记录每个状态更改。像是在短时间多次提交到 store 或轮询,并不总是需要跟踪。在这种情况下你可以在 `store.commit` 中传入第三个参数,来指定插件中的 mutation 是否“静默”。
8787

8888
``` js
8989
store.commit('increment', {
9090
amount: 1
9191
}, { silent: true })
9292

93-
// with object-style dispatch
93+
// 使用对象风格的 dispatch
9494
store.commit({
9595
type: 'increment',
9696
amount: 1
9797
}, { silent: true })
9898
```
9999

100-
### Mutations Follow Vue's Reactivity Rules
100+
### 遵循 Vue 响应式规则
101101

102-
Since a Vuex store's state is made reactive by Vue, when we mutate the state, Vue components observing the state will update automatically. This also means Vuex mutations are subject to the same reactivity caveats when working with plain Vue:
102+
由于 Vue 中 Vuex store 的状态是响应式的,当我们改变状态,Vue 组件观察到状态改变将自动更新。这也意味着 Vuex mutation 同样遵循纯 Vue 响应式规则。
103103

104-
1. Prefer initializing your store's initial state with all desired fields upfront.
104+
1. 推荐预先初始化 store 中你所需的初始状态。
105105

106-
2. When adding new properties to an Object, you should either:
106+
2. 向对象添加新的属性时,你应该这样做:
107107

108-
- Use `Vue.set(obj, 'newProp', 123)`, or -
108+
- 使用 `Vue.set(obj, 'newProp', 123)`, -
109109

110-
- Replace that Object with a fresh one. For example, using the stage-2 [object spread syntax](https://github.com/sebmarkbage/ecmascript-rest-spread) we can write it like this:
110+
- 用新的对象替换该对象。例如,使用 stage-2 [对象扩展语法](https://github.com/sebmarkbage/ecmascript-rest-spread) 我们可以这样写:
111111

112112
``` js
113113
state.obj = { ...state.obj, newProp: 123 }
114114
```
115115

116-
### Using Constants for Mutation Names
116+
### 用常量命名 Mutation
117117

118-
It is a commonly seen pattern to use constants for mutation types in various Flux implementations. This allow the code to take advantage of tooling like linters, and putting all constants in a single file allows your collaborators to get an at-a-glance view of what mutations are possible in the entire application:
118+
在各种 Flux 实现中,使用常量作为 mutation 类型是一种常见的模式。这允许代码利用工具如 linters,将所有常量放在一个单独文件中,尽可能使协作者对整个应用的 mutation 一目了然。
119119

120120
``` js
121121
// mutation-types.js
@@ -130,20 +130,19 @@ import { SOME_MUTATION } from './mutation-types'
130130
const store = new Vuex.Store({
131131
state: { ... },
132132
mutations: {
133-
// we can use the ES2015 computed property name feature
134-
// to use a constant as the function name
133+
// 我们能够通过使用“ES2015 属性名表达式”功能,来使用常量作为函数名称
135134
[SOME_MUTATION] (state) {
136-
// mutate state
135+
// 改变状态
137136
}
138137
}
139138
})
140139
```
141140

142-
Whether to use constants is largely a preference - it can be helpful in large projects with many developers, but it's totally optional if you don't like them.
141+
是否使用常量在很大程度上是一个偏好 - 在多人合作开发的大型项目中它很有用,但如果你不喜欢使用,它也是完全可选的。
143142

144-
### Mutations Must Be Synchronous
143+
### Mutation 必须是同步函数
145144

146-
One important rule to remember is that **mutation handler functions must be synchronous**. Why? Consider the following example:
145+
一个重要的原则就是牢记 **mutation 必须是同步函数**。为什么?考虑下面的例子:
147146

148147
``` js
149148
mutations: {
@@ -155,11 +154,11 @@ mutations: {
155154
}
156155
```
157156

158-
Now imagine we are debugging the app and looking at the devtool's mutation logs. For every mutation logged, the devtool will need to capture a "before" and "after" snapshots of the state. However, the asynchronous callback inside the example mutation above makes that impossible: the callback is not called yet when the mutation is committed, and there's no way for the devtool to know when the callback will actually be called - any state mutation performed in the callback is essentially un-trackable!
157+
现在想象我们正在调试应用程序,并查看 devtoolmutation 记录。每个 mutation 记录,devtool 将需要捕获每个状态“之前”和“之后”的快照。然而,上面的示例中 mutation 内部的异步回调使得这是不可能的:当 mutation 被提交后,回调函数还未被调用,也没有办法让 devtool 知道回调函数在何时被调用 - 即在回调函数中执行任意状态变更,实际上都无法跟踪。
159158

160-
### Commiting Mutations in Components
159+
### 组件中提交 Mutation
161160

162-
You can commit mutations in components with `this.$store.commit('xxx')`, or use the `mapMutations` helper which maps component methods to `store.commit` calls (requires root `store` injection):
161+
可以在组件中使用 `this.$store.commit('xxx')` 提交 mutation,或者使用 `mapMutations` 工具遍历组件方法到 `store.commit` 的回调上(需要把 `store` 注入根组件)
163162

164163
``` js
165164
import { mapMutations } from 'vuex'
@@ -168,23 +167,22 @@ export default {
168167
// ...
169168
methods: {
170169
...mapMutations([
171-
'increment' // map this.increment() to this.$store.commit('increment')
170+
'increment' // 映射 this.increment() this.$store.commit('increment')
172171
]),
173172
...mapMutations({
174-
add: 'increment' // map this.add() to this.$store.commit('increment')
173+
add: 'increment' // 映射 this.add() this.$store.commit('increment')
175174
})
176175
}
177176
}
178177
```
179178

180-
### On to Actions
179+
### 下一步:Actions
181180

182-
Asynchronicity combined with state mutation can make your program very hard to reason about. For example, when you call two methods both with async callbacks that mutate the state, how do you know when they are called and which callback was called first? This is exactly why we want to separate the two concepts. In Vuex, **mutations are synchronous transactions**:
181+
mutation 中混合异步调用会导致你的程序很难调试。例如当你调用两个都含有异步回调的方法去改变状态,你如何知道他们何时被调用和哪个回调被首先调用?这正是我们分离 Mutation 和 Action 这两个概念的原因。在 Vuex**Mutation 必须是同步事务**
183182

184183
``` js
185184
store.commit('increment')
186-
// any state change that the "increment" mutation may cause
187-
// should be done at this moment.
185+
// 类型为 "increment" 的 mutation 提交后,可能引起的任意状态变化,都应该在此时同步完成
188186
```
189187

190-
To handle asynchronous operations, let's introduce [Actions](actions.md).
188+
为了处理异步操作,接下来我们介绍 [Actions](actions.html)。

src/vuex/state.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ computed: mapState([
9595

9696
### 对象扩展运算符
9797

98-
注意,`mapState` 返回一个对象。我们如何使用 mapState 合并其他局部的计算属性呢?通常地,为了将多个对象合并为一个对象,再传入这个合并好的最终对象到 `computed` 属性去,我们不得不使用一个工具函数来实现。然而有了[对象扩展运算符](https://github.com/sebmarkbage/ecmascript-rest-spread)ECMAScript标准提案 stage-3),我们就能够让语法变得简洁起来:
98+
注意,`mapState` 返回一个对象。我们如何使用 mapState 合并其他局部的计算属性呢?通常地,为了将多个对象合并为一个对象,再把这个合并好的最终对象传入到 `computed` 属性去,我们不得不使用一个工具函数来实现。然而有了[对象扩展运算符](https://github.com/sebmarkbage/ecmascript-rest-spread)ECMAScript 标准提案 stage-3),我们就能够让语法变得简洁起来:
9999

100100
``` js
101101
computed: {

0 commit comments

Comments
 (0)