|
| 1 | +--- |
| 2 | +title: Migration from Vuex 0.6.x to 1.0 |
| 3 | +type: guide |
| 4 | +order: 27 |
| 5 | +--- |
| 6 | + |
| 7 | +> Vuex 2.0 is released, but this guide only covers the migration to 1.0? Is that a typo? Also, it looks like Vuex 1.0 and 2.0 were released simultaneously. What's going on? Which one should I use and what's compatible with Vue 2.0? |
| 8 | +
|
| 9 | +Both Vuex 1.0 and 2.0: |
| 10 | + |
| 11 | +- fully support both Vue 1.0 and 2.0 |
| 12 | +- will be maintained for the forseeable future |
| 13 | + |
| 14 | +They have slightly different target users however. |
| 15 | + |
| 16 | +__Vuex 2.0__ is a radical redesign and simplification of the API, for those who are starting new projects or want to be on the cutting edge of client-side state management. __It is not covered by this migration guide__, so you should check out [the Vuex 2.0 docs](https://vuex.vuejs.org/en/index.html) if you'd like to learn more about it. |
| 17 | + |
| 18 | +__Vuex 1.0__ is mostly backwards-compatible, so requires very few changes to upgrade. It is recommended for those with large existing codebases or who just want the smoothest possible upgrade path to Vue 2.0. This guide is dedicated to facilitating that process, but only includes migration notes. For the complete usage guide, see [the Vuex 1.0 docs](https://github.com/vuejs/vuex/tree/1.0/docs/en). |
| 19 | + |
| 20 | +<p class="tip">The list of deprecations below should be relatively complete, but the migration helper is still being updated to catch them.</p> |
| 21 | + |
| 22 | +## `store.watch` with String Property Path <sup>deprecated</sup> |
| 23 | + |
| 24 | +`store.watch` now only accept functions. So for example, you would have to replace: |
| 25 | + |
| 26 | +``` js |
| 27 | +store.watch('user.notifications', callback) |
| 28 | +``` |
| 29 | + |
| 30 | +with: |
| 31 | + |
| 32 | +``` js |
| 33 | +store.watch( |
| 34 | + // When the returned result changes... |
| 35 | + function (state) { |
| 36 | + return state.user.notifications |
| 37 | + }, |
| 38 | + // Run this callback |
| 39 | + callback |
| 40 | +) |
| 41 | +``` |
| 42 | + |
| 43 | +This gives you more complete control over the reactive properties you'd like to watch. |
| 44 | + |
| 45 | +{% raw %} |
| 46 | +<div class="upgrade-path"> |
| 47 | + <h4>Upgrade Path</h4> |
| 48 | + <p>Run the <a href="https://github.com/vuejs/vue-migration-helper">migration helper</a> on your codebase to find examples of <code>store.watch</code> with a string as the first argument.</p> |
| 49 | +</div> |
| 50 | +{% endraw %} |
| 51 | + |
| 52 | +## Store's Event Emitter <sup>deprecated</sup> |
| 53 | + |
| 54 | +The store instance no longer exposes the event emitter interface (`on`, `off`, `emit`). If you were previously using the store as a global event bus, [see this section](http://vuejs.org/guide/migration.html#dispatch-and-broadcast-deprecated) for migration instructions. |
| 55 | + |
| 56 | +Instead of using this interface to watch events emitted by the store itself (e.g. `store.on('mutation', callback)`), a new method `store.subscribe` is introduced. Typical usage inside a plugin would be: |
| 57 | + |
| 58 | +``` js |
| 59 | +var myPlugin = store => { |
| 60 | + store.subscribe(function (mutation, state) { |
| 61 | + // Do something... |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +``` |
| 66 | + |
| 67 | +See example [the plugins docs](https://github.com/vuejs/vuex/blob/1.0/docs/en/plugins.md) for more info. |
| 68 | + |
| 69 | +{% raw %} |
| 70 | +<div class="upgrade-path"> |
| 71 | + <h4>Upgrade Path</h4> |
| 72 | + <p>Run the <a href="https://github.com/vuejs/vue-migration-helper">migration helper</a> on your codebase to find examples of <code>store.on</code>, <code>store.off</code>, and <code>store.emit</code>.</p> |
| 73 | +</div> |
| 74 | +{% endraw %} |
| 75 | + |
| 76 | +## Middlewares <sup>deprecated</sup> |
| 77 | + |
| 78 | +Middlewares are replaced by plugins. A plugin is simply a function that receives the store as the only argument, and can listen to the mutation event on the store: |
| 79 | + |
| 80 | +``` js |
| 81 | +const myPlugins = store => { |
| 82 | + store.subscribe('mutation', (mutation, state) => { |
| 83 | + // Do something... |
| 84 | + }) |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +For more details, see [the plugins docs](https://github.com/vuejs/vuex/blob/1.0/docs/en/plugins.md). |
| 89 | + |
| 90 | +{% raw %} |
| 91 | +<div class="upgrade-path"> |
| 92 | + <h4>Upgrade Path</h4> |
| 93 | + <p>Run the <a href="https://github.com/vuejs/vue-migration-helper">migration helper</a> on your codebase to find examples of the <code>middlewares</code> option on a store.</p> |
| 94 | +</div> |
| 95 | +{% endraw %} |
0 commit comments