Close
Vue 3 docs | Vue 2.7 | Extended LTS

Migration from Vuex 0.6.x to 1.0

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?

Both Vuex 1.0 and 2.0:

They have slightly different target users however.

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 if you’d like to learn more about it.

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 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.

store.watch with String Property Path replaced

store.watch now only accept functions. So for example, you would have to replace:

store.watch('user.notifications', callback)

with:

store.watch(
// When the returned result changes...
function (state) {
return state.user.notifications
},
// Run this callback
callback
)

This gives you more complete control over the reactive properties you’d like to watch.

Upgrade Path

Run the migration helper on your codebase to find examples of store.watch with a string as the first argument.

Store’s Event Emitter removed

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 for migration instructions.

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:

var myPlugin = store => {
store.subscribe(function (mutation, state) {
// Do something...
})
}

See example the plugins docs for more info.

Upgrade Path

Run the migration helper on your codebase to find examples of store.on, store.off, and store.emit.

Middlewares replaced

Middlewares are replaced by plugins. A plugin is a function that receives the store as the only argument, and can listen to the mutation event on the store:

const myPlugins = store => {
store.subscribe('mutation', (mutation, state) => {
// Do something...
})
}

For more details, see the plugins docs.

Upgrade Path

Run the migration helper on your codebase to find examples of the middlewares option on a store.