Skip to content

本地状态 => 局部状态 #453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/zh-cn/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const store = new Vuex.Store({ ...options })

- 类型: `{ [type: string]: Function }`

在 store 上注册 mutation,处理函数总是接受 `state` 作为第一个参数(如果定义在模块中,则为模块的本地状态),`payload` 作为第二个参数(可选)。
在 store 上注册 mutation,处理函数总是接受 `state` 作为第一个参数(如果定义在模块中,则为模块的局部状态),`payload` 作为第二个参数(可选)。

[详细介绍](mutations.md)

Expand All @@ -34,7 +34,7 @@ const store = new Vuex.Store({ ...options })

``` js
{
state, // 等同于 store.state, 若在模块中则为本地状态
state, // 等同于 store.state, 若在模块中则为局部状态
rootState, // 等同于 store.state, 只存在于模块中
commit, // 等同于 store.commit
dispatch, // 等同于 store.dispatch
Expand All @@ -49,9 +49,9 @@ const store = new Vuex.Store({ ...options })
- 类型: `{ [key: string]: Function }`

在 store 上注册 getter,getter 方法接受以下参数:

```
state, // 如果在模块中定义则为模块的本地状态
state, // 如果在模块中定义则为模块的局部状态
getters, // 等同于 store.getters
rootState // 等同于 store.state
```
Expand All @@ -78,7 +78,7 @@ const store = new Vuex.Store({ ...options })
}
```

与根模块的选项一样,每个模块也包含 `state` 和 `mutations` 选项。模块的状态使用 key 关联到 store 的根状态。模块的 mutation 和 getter 只会接收 module 的本地状态作为第一个参数,而不是根状态,并且模块 action 的 `context.state` 同样指向本地状态
与根模块的选项一样,每个模块也包含 `state` 和 `mutations` 选项。模块的状态使用 key 关联到 store 的根状态。模块的 mutation 和 getter 只会接收 module 的局部状态作为第一个参数,而不是根状态,并且模块 action 的 `context.state` 同样指向局部状态

[详细介绍](modules.md)

Expand Down
2 changes: 1 addition & 1 deletion docs/zh-cn/forms.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mutations: {

### 双向绑定的计算属性

必须承认,这样做比简单地使用“`v-model` + 本地状态”要啰嗦得多,并且也损失了一些 `v-model` 中很有用的特性。另一个方法是使用带有 setter 的双向绑定计算属性:
必须承认,这样做比简单地使用“`v-model` + 局部状态”要啰嗦得多,并且也损失了一些 `v-model` 中很有用的特性。另一个方法是使用带有 setter 的双向绑定计算属性:

``` js
// ...
Expand Down
2 changes: 1 addition & 1 deletion docs/zh-cn/getters.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ computed: {

### `mapGetters` 辅助函数

`mapGetters` 辅助函数仅仅是将 store 中的 getters 映射到 本地计算属性
`mapGetters` 辅助函数仅仅是将 store 中的 getters 映射到局部计算属性

``` js
import { mapGetters } from 'vuex'
Expand Down
8 changes: 4 additions & 4 deletions docs/zh-cn/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ store.state.a // -> moduleA 的状态
store.state.b // -> moduleB 的状态
```

### 模块的本地状态
### 模块的局部状态

对于模块内部的 mutation 和 getter,接收的第一个参数是**模块的本地状态**。
对于模块内部的 mutation 和 getter,接收的第一个参数是**模块的局部状态**。

``` js
const moduleA = {
state: { count: 0 },
mutations: {
increment: (state) {
// state 模块的本地状态
// state 模块的局部状态
state.count++
}
},
Expand All @@ -51,7 +51,7 @@ const moduleA = {
}
```

同样,对于模块内部的 action,`context.state` 是本地状态,根节点的状态是 `context.rootState`:
同样,对于模块内部的 action,`context.state` 是局部状态,根节点的状态是 `context.rootState`:

``` js
const moduleA = {
Expand Down
8 changes: 4 additions & 4 deletions docs/zh-cn/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default {
// 传字符串参数 'count' 等同于 `state => state.count`
countAlias: 'count',

// 为了能够使用 `this` 获取本地状态,必须使用常规函数
// 为了能够使用 `this` 获取局部状态,必须使用常规函数
countPlusLocalState (state) {
return state.count + this.localCount
}
Expand All @@ -91,7 +91,7 @@ computed: mapState([

### 对象展开运算符

`mapState` 函数返回的是一个对象。我们如何将它与本地计算属性混合使用呢?通常,我们需要使用一个工具函数将多个对象合并为一个,以使我们可以将最终对象传给 `computed` 属性。但是自从有了[对象展开运算符](https://github.com/sebmarkbage/ecmascript-rest-spread)(现处于 ECMASCript 提案 stage-3 阶段),我们可以极大地简化写法:
`mapState` 函数返回的是一个对象。我们如何将它与局部计算属性混合使用呢?通常,我们需要使用一个工具函数将多个对象合并为一个,以使我们可以将最终对象传给 `computed` 属性。但是自从有了[对象展开运算符](https://github.com/sebmarkbage/ecmascript-rest-spread)(现处于 ECMASCript 提案 stage-3 阶段),我们可以极大地简化写法:

``` js
computed: {
Expand All @@ -103,6 +103,6 @@ computed: {
}
```

### 组件仍然保有本地状态
### 组件仍然保有局部状态

使用 Vuex 并不意味着你需要将**所有的**状态放入 Vuex。虽然将所有的状态放到 Vuex 会使状态变化更显式和易调试,但也会使代码变得冗长和不直观。如果有些状态严格属于单个组件,最好还是作为组件的本地状态。你应该根据你的应用开发需要进行权衡和确定。
使用 Vuex 并不意味着你需要将**所有的**状态放入 Vuex。虽然将所有的状态放到 Vuex 会使状态变化更显式和易调试,但也会使代码变得冗长和不直观。如果有些状态严格属于单个组件,最好还是作为组件的局部状态。你应该根据你的应用开发需要进行权衡和确定。