|
| 1 | +# Plugins |
| 2 | + |
| 3 | +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: |
| 4 | + |
| 5 | +``` js |
| 6 | +const myPlugin = store => { |
| 7 | + // called when the store is initialized |
| 8 | + store.on('mutation', (mutation, state) => { |
| 9 | + // called after every mutation. |
| 10 | + // The mutation comes in the format of { type, payload } for normal |
| 11 | + // dispatches, and will be the original mutation object for object-style |
| 12 | + // dispatches. |
| 13 | + }) |
| 14 | +} |
| 15 | +``` |
| 16 | + |
| 17 | +And can be used like this: |
| 18 | + |
| 19 | +``` js |
| 20 | +const store = new Vuex.Store({ |
| 21 | + // ... |
| 22 | + plugins: [myPlugin] |
| 23 | +}) |
| 24 | +``` |
| 25 | + |
| 26 | +Plugins are not allowed to directly mutate state - similar to your components, they can only trigger changes by dispatching mutations. |
| 27 | + |
| 28 | +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: |
| 29 | + |
| 30 | +``` js |
| 31 | +const myPluginWithSnapshot = store => { |
| 32 | + let prevState = _.cloneDeep(store.state) |
| 33 | + store.on('mutation', (mutation, state) => { |
| 34 | + let nextState = _.cloneDeep(state) |
| 35 | + |
| 36 | + // compare prevState and nextState... |
| 37 | + |
| 38 | + // save state for next mutation |
| 39 | + prevState = nextState |
| 40 | + }) |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +**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: |
| 45 | + |
| 46 | +``` js |
| 47 | +const store = new Vuex.Store({ |
| 48 | + // ... |
| 49 | + plugins: process.env.NODE_ENV !== 'production' |
| 50 | + ? [myPluginWithSnapshot] |
| 51 | + : [] |
| 52 | +}) |
| 53 | +``` |
| 54 | + |
| 55 | +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. |
| 56 | + |
| 57 | +### Built-in Logger Plugin |
| 58 | + |
| 59 | +Vuex comes with a logger plugin for common debugging usage: |
| 60 | + |
| 61 | +``` js |
| 62 | +import createLogger from 'vuex/logger' |
| 63 | + |
| 64 | +const store = new Vuex.Store({ |
| 65 | + plugins: [createLogger()] |
| 66 | +}) |
| 67 | +``` |
| 68 | + |
| 69 | +The `createLogger` function takes a few options: |
| 70 | + |
| 71 | +``` js |
| 72 | +const logger = createLogger({ |
| 73 | + collapsed: false, // auto-expand logged mutations |
| 74 | + transformer (state) { |
| 75 | + // transform the state before logging it. |
| 76 | + // for example return only a specific sub-tree |
| 77 | + return state.subTree |
| 78 | + }, |
| 79 | + mutationTransformer (mutation) { |
| 80 | + // mutations are logged in the format of { type, payload } |
| 81 | + // we can format it any way we want. |
| 82 | + return mutation.type |
| 83 | + } |
| 84 | +}) |
| 85 | +``` |
| 86 | + |
| 87 | +Note the logger plugin takes state snapshots, so use it only during development. |
0 commit comments