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/vuex/plugins.md
+25-26Lines changed: 25 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -6,19 +6,18 @@ order: 10
6
6
7
7
# Plugins
8
8
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 作为唯一参数:
10
10
11
11
```js
12
12
constmyPlugin=store=> {
13
-
//called when the store is initialized
13
+
//当 store 在被初始化完成时被调用
14
14
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}。
17
17
})
18
18
}
19
19
```
20
-
21
-
And can be used like this:
20
+
然后像这样使用:
22
21
23
22
```js
24
23
conststore=newVuex.Store({
@@ -27,11 +26,11 @@ const store = new Vuex.Store({
27
26
})
28
27
```
29
28
30
-
### Committing Mutations Inside Plugins
29
+
### 在插件内提交 Mutations
31
30
32
-
Plugins are not allowed to directly mutate state - similar to your components, they can only trigger changes by committing mutations.
31
+
插件不能直接修改状态 - 这就像你的组件,它们只能被 mutations 来触发改变。
33
32
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`方法会附加更多的可选项,来完成复杂的任务)。
@@ -58,25 +57,25 @@ const store = new Vuex.Store({
58
57
})
59
58
```
60
59
61
-
### Taking State Snapshots
60
+
### 生成状态快照
62
61
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:
**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:
@@ -87,13 +86,13 @@ const store = new Vuex.Store({
87
86
})
88
87
```
89
88
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.
Copy file name to clipboardExpand all lines: src/vuex/strict.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,9 @@ type: vuex
4
4
order: 11
5
5
---
6
6
7
-
# Strict Mode
7
+
# 严格模式
8
8
9
-
To enable strict mode, simply pass in `strict: true` when creating a Vuex store:
9
+
要启用严格模式,只需在创建 Vuex store 的时候简单地传入 strict: true。
10
10
11
11
```js
12
12
conststore=newVuex.Store({
@@ -15,13 +15,13 @@ const store = new Vuex.Store({
15
15
})
16
16
```
17
17
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.
**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.
0 commit comments