|
@@ -1,20 +1,20 @@
|
|
-# Middlewares
|
|
|
|
|
|
+# 中间件
|
|
|
|
|
|
-Vuex instances accept the `middlewares` option that exposes hooks for each mutation (Note this is completely unrelated to Redux middlewares). A Vuex middleware is simply an object that implements some hook functions:
|
|
|
|
|
|
+Vuex 实例可以接受 `middlewares`。中间件给每一个 mutation 暴露勾子(注意和 Redux 的中间件完全没有关系)。一个 Vuex 中间件是一个包含一些勾子函数的简单对象:
|
|
|
|
|
|
``` js
|
|
``` js
|
|
const myMiddleware = {
|
|
const myMiddleware = {
|
|
onInit (state) {
|
|
onInit (state) {
|
|
- // record initial state
|
|
|
|
|
|
+ // 记录初始 state
|
|
},
|
|
},
|
|
onMutation (mutation, state) {
|
|
onMutation (mutation, state) {
|
|
- // called after every mutation.
|
|
|
|
- // The mutation comes in the format of { type, payload }
|
|
|
|
|
|
+ // 每个 mutation 后会被调用
|
|
|
|
+ // mutation 在这里会被格式化为 { type, payload }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
```
|
|
```
|
|
|
|
|
|
-And can be used like this:
|
|
|
|
|
|
+可以这样去使用中间件:
|
|
|
|
|
|
``` js
|
|
``` js
|
|
const vuex = new Vuex({
|
|
const vuex = new Vuex({
|
|
@@ -23,9 +23,9 @@ const vuex = new Vuex({
|
|
})
|
|
})
|
|
```
|
|
```
|
|
|
|
|
|
-By default, a middleware receives the actual `state` object. Since middlewares are primarily used for debugging purposes, they are **not allowed to mutate the state**.
|
|
|
|
|
|
+一个中间件默认会接受一个真实的 `state` 对象,但中间件其实用于 debugging, 他们是 **不允许改变 state 的**
|
|
|
|
|
|
-Sometimes a middleware may want to receive "snapshots" of the state, and also compare the post-mutation state with pre-mutation state. Such middlewares must declare the `snapshot: true` option:
|
|
|
|
|
|
+有时候中间件想要获得 state 的快照(snapshots),并且用来比较 mutation 前后的 state。这样的中间件必须定义 `snapshot: true` 选项:
|
|
|
|
|
|
``` js
|
|
``` js
|
|
const myMiddlewareWithSnapshot = {
|
|
const myMiddlewareWithSnapshot = {
|
|
@@ -37,7 +37,7 @@ const myMiddlewareWithSnapshot = {
|
|
}
|
|
}
|
|
```
|
|
```
|
|
|
|
|
|
-**Middlewares 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:
|
|
|
|
|
|
+**可以获得快照的中间件只能在开发环境下使用**。使用 Webpack 或 Browserify 时,我们可以让 build tools 帮我们处理这个事情:
|
|
|
|
|
|
``` js
|
|
``` js
|
|
const vuex = new Vuex({
|
|
const vuex = new Vuex({
|
|
@@ -48,11 +48,11 @@ const vuex = new Vuex({
|
|
})
|
|
})
|
|
```
|
|
```
|
|
|
|
|
|
-The middleware will be used by default. For production, use the build setup described [here](http://vuejs.org/guide/application.html#Deploying_for_Production) to convert the value of `process.env.NODE_ENV !== 'production'` to `false` for the final build.
|
|
|
|
|
|
+中间件默认会被使用。在最终的生产环境下,请根据 [这里](http://vuejs.org/guide/application.html#Deploying_for_Production) 把 `process.env.NODE_ENV !== 'production'` 替换为 `false`。
|
|
|
|
|
|
-### Built-in Logger Middleware
|
|
|
|
|
|
+### 内置的 logger 中间件
|
|
|
|
|
|
-Veux comes with a logger middleware for common debugging usage:
|
|
|
|
|
|
+Vuex 有个自带的 logger 中间件用于 debugging:
|
|
|
|
|
|
``` js
|
|
``` js
|
|
const vuex = new Vuex({
|
|
const vuex = new Vuex({
|
|
@@ -60,22 +60,22 @@ const vuex = new Vuex({
|
|
})
|
|
})
|
|
```
|
|
```
|
|
|
|
|
|
-The `createLogger` function takes a few options:
|
|
|
|
|
|
+`createLogger` 函数有这几个 options:
|
|
|
|
|
|
``` js
|
|
``` js
|
|
const logger = Vuex.createLogger({
|
|
const logger = Vuex.createLogger({
|
|
- collapsed: false, // auto-expand logged mutations
|
|
|
|
|
|
+ collapsed: false, // 自动展开输出的 mutations
|
|
transformer (state) {
|
|
transformer (state) {
|
|
- // transform the state before logging it.
|
|
|
|
- // for example return only a specific sub-tree
|
|
|
|
|
|
+ // 输出前对 state 进行转换
|
|
|
|
+ // 比如说只返回一个 sub-tree
|
|
return state.subTree
|
|
return state.subTree
|
|
},
|
|
},
|
|
mutationTransformer (mutation) {
|
|
mutationTransformer (mutation) {
|
|
- // mutations are logged in the format of { type, payload }
|
|
|
|
- // we can format it anyway we want.
|
|
|
|
|
|
+ // mutations 会格式化为 { type, payload } 输出
|
|
|
|
+ // 我们可以按照自己的需求格式化成任何我们想要的结构
|
|
return mutation.type
|
|
return mutation.type
|
|
}
|
|
}
|
|
})
|
|
})
|
|
```
|
|
```
|
|
|
|
|
|
-Note the logger middleware takes state snapshots, so use it only during development.
|
|
|
|
|
|
+注意这个 logger 中间件会得到 state 快照,所以只能用于开发环境。
|