Skip to content

Commit 4eeb210

Browse files
authored
Merge pull request #130 from yongbolv/2.0-cn
transter plugins.md and strict.md
2 parents a2f22ec + eb1c87f commit 4eeb210

File tree

2 files changed

+31
-32
lines changed

2 files changed

+31
-32
lines changed

src/vuex/plugins.md

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ order: 10
66

77
# Plugins
88

9-
Vuex stores accept the `plugins` option that exposes hooks for each mutation. A Vuex plugin is simply a function that receives the store as the only argument:
9+
Vuex 的 store 接收 `plugins` 选项,这个选项暴露出每个 mutation 的钩子。一个 Vuex 的插件就是一个简单的方法,接收 sotre 作为唯一参数:
1010

1111
``` js
1212
const myPlugin = store => {
13-
// called when the store is initialized
13+
// store 在被初始化完成时被调用
1414
store.subscribe((mutation, state) => {
15-
// called after every mutation.
16-
// The mutation comes in the format of { type, payload }.
15+
// mutation 之后被调用
16+
// mutation 的格式为 {type, payload}。
1717
})
1818
}
1919
```
20-
21-
And can be used like this:
20+
然后像这样使用:
2221

2322
``` js
2423
const store = new Vuex.Store({
@@ -27,11 +26,11 @@ const store = new Vuex.Store({
2726
})
2827
```
2928

30-
### Committing Mutations Inside Plugins
29+
### 在插件内提交 Mutations
3130

32-
Plugins are not allowed to directly mutate state - similar to your components, they can only trigger changes by committing mutations.
31+
插件不能直接修改状态 - 这就像你的组件,它们只能被 mutations 来触发改变。
3332

34-
By committing mutations, a plugin can be used to sync a data source to the store. For example, to sync a websocket data source to the store (this is just a contrived example, in reality the `createPlugin` function can take some additional options for more complex tasks):
33+
通过提交 mutations,插件可以用来同步数据源到 store。例如, 为了同步 websocket 数据源到 store (这只是为说明用法的例子,在实际中,`createPlugin` 方法会附加更多的可选项,来完成复杂的任务)。
3534

3635
``` js
3736
export default function createWebSocketPlugin (socket) {
@@ -58,25 +57,25 @@ const store = new Vuex.Store({
5857
})
5958
```
6059

61-
### Taking State Snapshots
60+
### 生成状态快照
6261

63-
Sometimes a plugin may want to receive "snapshots" of the state, and also compare the post-mutation state with pre-mutation state. To achieve that, you will need to perform a deep-copy on the state object:
62+
有时候插件想获取状态 “快照” 和状态的改变前后的变化。为了实现这些功能,需要对状态对象进行深拷贝:
6463

6564
``` js
6665
const myPluginWithSnapshot = store => {
6766
let prevState = _.cloneDeep(store.state)
6867
store.subscribe((mutation, state) => {
6968
let nextState = _.cloneDeep(state)
7069

71-
// compare prevState and nextState...
70+
// 对比 prevState nextState...
7271

73-
// save state for next mutation
72+
// 保存状态,用于下一次 mutation
7473
prevState = nextState
7574
})
7675
}
7776
```
7877

79-
**Plugins that take state snapshots should be used only during development.** When using Webpack or Browserify, we can let our build tools handle that for us:
78+
** 生成状态快照的插件只能在开发阶段使用,使用 Webpack Browserify,让构建工具帮我们处理:
8079

8180
``` js
8281
const store = new Vuex.Store({
@@ -87,13 +86,13 @@ const store = new Vuex.Store({
8786
})
8887
```
8988

90-
The plugin will be used by default. For production, you will need [DefinePlugin](https://webpack.github.io/docs/list-of-plugins.html#defineplugin) for Webpack or [envify](https://github.com/hughsk/envify) for Browserify to convert the value of `process.env.NODE_ENV !== 'production'` to `false` for the final build.
89+
插件默认会被起用。为了发布产品,你需要用 Webpack 的 [DefinePlugin](https://webpack.github.io/docs/list-of-plugins.html#defineplugin) 或者 Browserify 的 [envify](https://github.com/hughsk/envify) 来转换 `process.env.NODE_ENV !== 'production'` 的值为 `false`
9190

92-
### Built-in Logger Plugin
91+
### 内置 Logger 插件
9392

94-
> If you are using [vue-devtools](https://github.com/vuejs/vue-devtools) you probably don't need this.
93+
> 如果你正在使用 [vue-devtools](https://github.com/vuejs/vue-devtools),你可能不需要。
9594
96-
Vuex comes with a logger plugin for common debugging usage:
95+
Vuex 带来一个日志插件用于一般的调试:
9796

9897
``` js
9998
import createLogger from 'vuex/dist/logger'
@@ -103,24 +102,24 @@ const store = new Vuex.Store({
103102
})
104103
```
105104

106-
The `createLogger` function takes a few options:
105+
`createLogger` 方法有几个配置项:
107106

108107
``` js
109108
const logger = createLogger({
110-
collapsed: false, // auto-expand logged mutations
109+
collapsed: false, // 自动展开记录 mutation
111110
transformer (state) {
112-
// transform the state before logging it.
113-
// for example return only a specific sub-tree
111+
// 在记录之前前进行转换
112+
// 例如,只返回指定的子树
114113
return state.subTree
115114
},
116115
mutationTransformer (mutation) {
117-
// mutations are logged in the format of { type, payload }
118-
// we can format it any way we want.
116+
// mutation 格式 { type, payload }
117+
// 我们可以按照想要的方式进行格式化
119118
return mutation.type
120119
}
121120
})
122121
```
123122

124-
The logger file can also be included directly via a `<script>` tag, and will expose the `createVuexLogger` function globally.
123+
日志插件还可以直接通过 `<script>` 标签, 然后它会提供全局方法 `createVuexLogger`
125124

126-
Note the logger plugin takes state snapshots, so use it only during development.
125+
要注意,logger 插件会生成状态快照,所以仅在开发环境使用。

src/vuex/strict.md

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

7-
# Strict Mode
7+
# 严格模式
88

9-
To enable strict mode, simply pass in `strict: true` when creating a Vuex store:
9+
要启用严格模式,只需在创建 Vuex store 的时候简单地传入 strict: true
1010

1111
``` js
1212
const store = new Vuex.Store({
@@ -15,13 +15,13 @@ const store = new Vuex.Store({
1515
})
1616
```
1717

18-
In strict mode, whenever Vuex state is mutated outside of mutation handlers, an error will be thrown. This ensures that all state mutations can be explicitly tracked by debugging tools.
18+
在严格模式下,只要 Vuex 状态在 mutation 方法外被修改就会抛出错误。这确保了所有状态修改都会明确的被调试工具跟踪。
1919

20-
### Development vs. Production
20+
### 开发阶段 vs. 发布阶段
2121

22-
**Do not enable strict mode when deploying for production!** Strict mode runs a deep watch on the state tree for detecting inappropriate mutations - make sure to turn it off in production to avoid the performance cost.
22+
** 不要在发布阶段开启严格模式! 严格模式会对状态树进行深度监测来检测不合适的修改 —— 确保在发布阶段关闭它避免性能损耗。
2323

24-
Similar to plugins, we can let the build tools handle that:
24+
跟处理插件的情况类似,我们可以让构建工具来处理:
2525

2626
``` js
2727
const store = new Vuex.Store({

0 commit comments

Comments
 (0)