|
@@ -1,24 +1,24 @@
|
|
|
-# Core Concepts
|
|
|
+# 核心概念
|
|
|
|
|
|
-Similar to Vue itself, Vuex exposes a single `Vuex` constructor. You can use it to create **Vuex instances**. In most cases, you only need one Vuex instance for an app. You can think of a Vuex instance as an "enhanced store" that holds your app state.
|
|
|
+和 Vue 一样,Vuex 暴露一个 `Vuex` 构造,你可以实例化一个 `Vuex 实例`。一般情况下,一个应用中只需要一个 Vuex 实例。你可以把它当成一个『额外的 store』来管理应用状态。
|
|
|
|
|
|
-Each Vuex instance consists of three types of "ingredients":
|
|
|
+每个 Vuex 实例由三个部分组成:
|
|
|
|
|
|
-- **State**: A plain object representing the application state.
|
|
|
+- **State**: 一个反映应用状态的纯对象。
|
|
|
|
|
|
-- **Mutations**: Functions that mutates the state. Mutations **must be synchronous**.
|
|
|
+- **Mutations**: 一些用于改变状态的函数。Mutations **一定是同步的**。
|
|
|
|
|
|
-- **Actions**: Functions that dispatch mutations. An action can contain asynchronous operations and can dispatch multiple mutations.
|
|
|
+- **Actions**: 一些用于dispatch mutations 的函数。一个 action 可以包含异步操作,并且可以 dispatch 多个 mutations.
|
|
|
|
|
|
-Why do we differentiate between *mutations* and *actions*, rather then just simple functions that manipulate the state however we want? The reason is because we want to **separate mutation and asynchronicity**. A lot of application complexity roots from the combination of the two. When separated, they both become easier to reason about and write tests for.
|
|
|
+我们把 *mutations* 和 *actions* 分开而不是直接用函数去操作 state,原因是我们希望把 mutation 和异步操作分离。许多应用之所以复杂,正是犹豫这两者的耦合。我们解耦两者后,它们都会变得清晰和易于测试。
|
|
|
|
|
|
-> If you are familiar with Flux, note there's a term/concept difference here: Vuex mutations are the equivalent of Flux **actions**, while Vuex actions are equivalent to Flux **action creators**.
|
|
|
+> 如果你熟悉 Flux,在这里要注意两者的一些差异:Vuex mutations 相当于 Flux 的 **actions**, 而 Vuex 的 actions 相当于 Flux 的 **action creators**.
|
|
|
|
|
|
-### Creating a Vuex Instance
|
|
|
+### 创建一个 Vuex 实例
|
|
|
|
|
|
-> **NOTE:** We will be using ES2015 syntax for code examples for the rest of the docs. If you haven't picked it up, [you should](https://babeljs.io/docs/learn-es2015/)! The doc also assumes you are already familiar with the concepts discussed in [Building Large-Scale Apps with Vue.js](http://vuejs.org/guide/application.html).
|
|
|
+> **NOTE:** 我们将用 ES2015 语法写接下来的事例代码。如果你还没开始接触 ES2015,那么你应该[现在就开始用](https://babeljs.io/docs/learn-es2015/)! 我们还默认你已经熟悉 [Building Large-Scale Apps with Vue.js](http://vuejs.org/guide/application.html) 中所提到的概念。
|
|
|
|
|
|
-Creating a Vuex instance is pretty straightforward - just put the aforementioned ingredients together:
|
|
|
+创建一个 Vuex 实例非常简单——只需要把以上提到的三个部分放到一起:
|
|
|
|
|
|
``` js
|
|
|
import Vuex from 'vuex'
|
|
@@ -30,5 +30,4 @@ const vuex = new Vuex({
|
|
|
})
|
|
|
```
|
|
|
|
|
|
-Once created, you can access the state via `vuex.state`, and the actions via `vuex.actions`. You cannot directly access the mutation functions - they can only be triggered by actions or calling `vuex.dispatch()`. We will discuss each concept in more details next.
|
|
|
-
|
|
|
+实例化后,你就能通过 `vuex.state` 访问 state, 通过 `vuex.actions` 访问 actions。你不能直接访问 mutation functions——你只能通过 actions 或执行 `vuex.disptach()` 访问。接下来我们会更详细地讨论这几个概念。
|