You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/state-management.md
+3-3Lines changed: 3 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ order: 21
6
6
7
7
## 类 Flux 状态管理的官方实现
8
8
9
-
由于多个状态分散的跨越在许多组件和交互间各个角落,大型应用复杂度也经常逐渐增长。为了解决这个问题,Vue 提供 [vuex](https://github.com/vuejs/vuex): 我们有受到 Elm 启发的状态管理库。vuex 甚至集成到 [vue-devtools](https://github.com/vuejs/vue-devtools),无需配置即可开始你的体验。
9
+
由于多个状态分散的跨越在许多组件和交互间各个角落,大型应用复杂度也经常逐渐增长。为了解决这个问题,Vue 提供 [vuex](https://github.com/vuejs/vuex): 我们有受到 Elm 启发的状态管理库。vuex 甚至集成到 [vue-devtools](https://github.com/vuejs/vue-devtools),无需配置即可访问时光旅行。
10
10
11
11
### React 的开发者请参考以下信息
12
12
@@ -49,7 +49,7 @@ var store = {
49
49
}
50
50
```
51
51
52
-
需要注意,所有 store 中 state 的改变,都放置在 store 自身的 action 中去管理。这种集中式状态管理能够被更容易地理解哪种类型的 mutations 将会发生,以及它们是如何被触发。当错误出现时,我们现在也会有一个 log 记录 bug 之前发生了什么。
52
+
需要注意,所有 store 中 state 的改变,都放置在 store 自身的 action 中去管理。这种集中式状态管理能够被更容易地理解哪种类型的 mutation 将会发生,以及它们是如何被触发。当错误出现时,我们现在也会有一个 log 记录 bug 之前发生了什么。
53
53
54
54
此外,每个实例/组件仍然可以拥有和管理自己的私有状态:
55
55
@@ -71,7 +71,7 @@ var vmB = new Vue({
71
71
72
72

73
73
74
-
<pclass="tip">重要的是,注意你不应该在 action 中 替换原始的状态对象 - 组件和 store 需要引用同一个共享对象,mutations 才能够被观察</p>
74
+
<pclass="tip">重要的是,注意你不应该在 action 中 替换原始的状态对象 - 组件和 store 需要引用同一个共享对象,mutation 才能够被观察</p>
75
75
76
76
接着我们继续延伸约定,组件不允许直接修改属于 store 实例的 state,而应执行 action 来分发 (dispatch) 事件通知 store 去改变,我们最终达成了 [Flux](https://facebook.github.io/flux/) 架构。这样约定的好处是,我们能够记录所有 store 中发生的 state 改变,同时实现能做到记录变更 (mutation) 、保存状态快照、历史回滚/时光旅行的先进的调试工具。
Copy file name to clipboardExpand all lines: src/vuex/state.md
+24-25Lines changed: 24 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -6,18 +6,18 @@ order: 4
6
6
7
7
# State
8
8
9
-
### Single State Tree
9
+
### 单一状态树
10
10
11
-
Vuex uses a **single state tree** - that is, this single object contains all your application level state and serves as the "single source of truth". This also means usually you will have only one store for each application. A single state tree makes it straightforward to locate a specific piece of state, and allows us to easily take snapshots of the current app state for debugging purposes.
11
+
Vuex 使用 **单一状态树** - 是的,用一个对象就包含了全部的应用层级状态,然后作为一个『唯一数据源(SSOT)』而存在。这也意味着,每一个应用将只有一个 store 实例。单一状态树让我们能够直接地定位任一特定的状态片段,在调试的过程中也能轻易地取得整个当前应用状态的快照。
12
12
13
-
The single state tree does not conflict with modularity - in later chapters we will discuss how to split your state and mutations into sub modules.
So how do we display state inside the store in our Vue components? Since Vuex stores are reactive, the simplest way to "retrieve" state from it is simply returning some store state from within a [computed property](http://vuejs.org/guide/computed.html):
17
+
那么我们如何在 Vue 组件中展示状态呢?由于 Vuex 的状态存储是响应式的,从 store 实例中读取状态最简单的方法,就是在[计算属性](http://vuejs.org/guide/computed.html)的函数中直接返回某个 store 的状态:
18
18
19
19
```js
20
-
//let's create a Counter component
20
+
//创建一个计数器组件
21
21
constCounter= {
22
22
template:`<div>{{ count }}</div>`,
23
23
computed: {
@@ -28,17 +28,16 @@ const Counter = {
28
28
}
29
29
```
30
30
31
-
Whenever`store.state.count`changes, it will cause the computed property to re-evaluate, and trigger associated DOM updates.
31
+
当`store.state.count`发生变化,将会引发重新对计算属性取值,并且相关联的 DOM 将触发更新。
32
32
33
-
However, this pattern causes the component to rely on the global store singleton. When using a module system, it requires importing the store in every component that uses store state, and also requires mocking when testing the component.
33
+
然而,这种模式导致组件依赖于全局状态单例。当使用模块系统时,还需要在每个组件都去引入 store,才能使每个组件都能使用 store 的状态,同时测试组件时也需要模拟出 store。
34
34
35
-
Vuex provides a mechanism to "inject" the store into all child components from the root component with the `store`option (enabled by `Vue.use(Vuex)`):
35
+
Vuex 提供一个机制,设置 `store`选项(启用`Vue.use(Vuex)`)将 store 从根组件『注入』到每一个子组件中:
36
36
37
37
```js
38
38
constapp=newVue({
39
39
el:'#app',
40
-
// provide the store using the "store" option.
41
-
// this will inject the store instance to all child components.
40
+
// 使用 "store" 选项后,可以注册 store 对象。将会把 store 实例注入到所有子组件。
42
41
store,
43
42
components: { Counter },
44
43
template:`
@@ -49,7 +48,7 @@ const app = new Vue({
49
48
})
50
49
```
51
50
52
-
By providing the `store`option to the root instance, the store will be injected into all child components of the root and will be available on them as `this.$store`. Let's update our `Counter` implementation:
51
+
通过在根实例中注册 `store`选项,该 store 实例会被注入到根组件下的所有子组件中,并且子组件可以通过 `this.$store` 来访问。让我们一起调整刚才 `计数器` 的实现:
53
52
54
53
```js
55
54
constCounter= {
@@ -62,54 +61,54 @@ const Counter = {
62
61
}
63
62
```
64
63
65
-
### The `mapState`Helper
64
+
### `mapState`工具
66
65
67
-
When a component needs to make use of multiple store state properties or getters, declaring all these computed properties can get repetitive and verbose. To deal with this we can make use of the `mapState`helper which generates computed getter functions for us and help us save some keystrokes:
66
+
当一个组件需要引用了 store 的多个 state 属性或 getter 函数,声明列举出所有计算属性会变得重复且繁琐。对此,在我们需要生成 computed 所需的很多个 getter 函数时,使用 `mapState`工具可以帮我们节省一些键盘按键(^_^):
68
67
69
68
```js
70
-
//in standalone builds helpers are exposed as Vuex.mapState
69
+
//vuex 提供了独立的构建工具函数 Vuex.mapState
71
70
import { mapState } from'vuex'
72
71
73
72
exportdefault {
74
73
// ...
75
74
computed:mapState({
76
-
//arrow functions can make the code very succinct!
75
+
//箭头函数可以让代码非常简洁
77
76
count:state=>state.count,
78
77
79
-
//passing the string value 'count' is same as `state => state.count`
78
+
//传入字符串 'count' 等同于 `state => state.count`
80
79
countAlias:'count',
81
80
82
-
//to access local state with `this`, a normal function must be used
81
+
//想访问局部状态,就必须借助于一个普通函数,函数中使用 `this` 获取局部状态
83
82
countPlusLocalState (state) {
84
83
returnstate.count+this.localCount
85
84
}
86
85
})
87
86
}
88
87
```
89
88
90
-
We can also pass a string array to `mapState`when the name of mapped computed property is same as state sub tree name.
Note that `mapState`returns an object. How do we use it in combination with other local computed properties? Normally, we'd have to use a utility to merge multiple objects into one so that we can pass the final object to `computed`. However with the [object spread operator](https://github.com/sebmarkbage/ecmascript-rest-spread) (which is a stage-3 ECMASCript proposal), we can greatly simplify the syntax:
//mix this into the outer object with the object spread operator
105
+
//使用对象扩展运算符,将 mapState 返回的对象和外层其他计算属性混合起来
107
106
...mapState({
108
107
// ...
109
108
})
110
109
}
111
110
```
112
111
113
-
### Components Can Still Have Local State
112
+
### 组件仍然可以具有局部状态
114
113
115
-
Using Vuex doesn't mean you should put **all**the state in Vuex. Although putting more state into Vuex makes your state mutations more explicit and debuggable, sometimes it could also make the code more verbose and indirect. If a piece of state strictly belongs to a single component, it could be just fine leaving it as local state. You should weigh the trade-offs and make decisions that fit the development needs of your app.
0 commit comments