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
+14-14Lines changed: 14 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -4,17 +4,17 @@ type: guide
4
4
order: 21
5
5
---
6
6
7
-
## Official Flux-Like Implementation
7
+
## 类 Flux 状态管理的官方实现
8
8
9
-
Large applications can often grow in complexity, due to multiple pieces of state scattered across many components and the interactions between them. To solve this problem, Vue offers[vuex](https://github.com/vuejs/vuex): our own Elm-inspired state management library. It even integrates into [vue-devtools](https://github.com/vuejs/vue-devtools), providing zero-setup access to time travel.
9
+
由于多个状态分散的跨越在许多组件和交互间各个角落,大型应用复杂度也经常逐渐增长。为了解决这个问题,Vue 提供[vuex](https://github.com/vuejs/vuex): 我们有受到 Elm 启发的状态管理库。vuex 甚至融入了 [vue-devtools](https://github.com/vuejs/vue-devtools),无需配置即可访问时光旅行。
10
10
11
-
### Information for React Developers
11
+
### React 的开发者请参考以下信息
12
12
13
-
If you're coming from React, you may be wondering how vuex compares to [redux](https://github.com/reactjs/redux), the most popular Flux implementation in that ecosystem. Redux is actually view-layer agnostic, so it can easily be used with Vue via some [simple bindings](https://github.com/egoist/revue). Vuex is different in that it _knows_ it's in a Vue app. This allows it to better integrate with Vue, offering a more intuitive API and improved development experience.
It is often overlooked that the source of truth in Vue applications is the raw `data` object - a Vue instance simply proxies access to it. Therefore, if you have a piece of state that should be shared by multiple instances, you can simply share it by identity:
Now whenever `sourceOfTruth`is mutated, both `vmA`and`vmB`will update their views automatically. Subcomponents within each of these instances would also have access via `this.$root.$data`. We have a single source of truth now, but debugging would be a nightmare. Any piece of data could be changed by any part of our app at any time, without leaving a trace.
To help solve this problem, we can adopt a simple **store pattern**:
33
+
如何解决这个问题呢?我们采用一个简单的 **store 模式**:
34
34
35
35
```js
36
36
var store = {
@@ -49,9 +49,9 @@ var store = {
49
49
}
50
50
```
51
51
52
-
Notice all actions that mutate the store's state are put inside the store itself. This type of centralized state management makes it easier to understand what type of mutations could happen and how are they triggered. When something goes wrong, we'll also now have a log of what happened leading up to the bug.
52
+
需要注意,所有 store 中 state 的改变,都放置在 store 自身的 action 中去管理。这种类型的集中式状态管理能够被更容易地理解,是哪部分 mutation 发生变更,以及 mutation 是如何被触发。当错误出现时,我们现在也会有一个 log 记录 bug 之前发生了什么。
53
53
54
-
In additon, each instance/component can still own and manage its own private state:
54
+
此外,每个实例/组件仍然可以拥有和管理自己的私有状态:
55
55
56
56
```js
57
57
var vmA =newVue({
@@ -69,10 +69,10 @@ var vmB = new Vue({
69
69
})
70
70
```
71
71
72
-

72
+

73
73
74
-
<pclass="tip">It's important to note that you should never replace the original state object in your actions - the components and the store need to share reference to the same object in order for mutations to be observed.</p>
74
+
<pclass="tip">重要的是,注意你不应该在 action 中 替换原始的状态对象 - 组件和 store 需要引用同一个共享对象,变更才能够被观察</p>
75
75
76
-
As we continue developing the convention where components are never allowed to directly mutate state that belongs to a store, but should instead dispatch events that notify the store to perform actions, we eventually arrive at the [Flux](https://facebook.github.io/flux/)architecture. The benefit of this convention is we can record all state mutations happening to the store and implement advanced debugging helpers such as mutation logs, snapshots, and history re-rolls / time travel.
76
+
接着我们继续延伸约定,组件不允许直接修改属于 store 实例的 state,而是应该使用分发 (dispatch) 事件通知 store 执行 action 作为替代,我们最终达成了 [Flux](https://facebook.github.io/flux/)架构。这样约定的好处是,我们能够记录所有 store 中发生的 state 改变,同时实现能做到记录变更 (mutation) 、保存状态快照、历史回滚/时光旅行的先进的调试工具。
77
77
78
-
This brings us full circle back to [vuex](https://github.com/vuejs/vuex), so if you've read this far it's probably time to try it out!
0 commit comments