Effortlessly keep vue-router and vuex store in sync.
JavaScript
Latest commit 35e85ab Jan 17, 2017 @yyx990803 yyx990803 4.1.1
Permalink
Failed to load latest commit information.
.gitignore init Mar 8, 2016
LICENSE Add License (#15) Jun 6, 2016
README.md go accepts number of steps back (#39) Dec 22, 2016
index.d.ts Return void from sync() (#41) Jan 17, 2017
index.js avoid using es6 features (fix #38) Dec 21, 2016
package.json 4.1.1 Jan 17, 2017

README.md

vuex-router-sync

Effortlessly keep vue-router and vuex store in sync.

Usage

# the latest version works only with vue-router >= 2.0
npm install vuex-router-sync

# for usage with vue-router < 2.0:
npm install vuex-router-sync@2
import { sync } from 'vuex-router-sync'
import store from './vuex/store' // vuex store instance
import router from './router' // vue-router instance

sync(store, router) // done.

// bootstrap your app...

You can set a custom vuex module name

sync(store, router, { moduleName: 'RouteModule' } )

How does it work?

  • It adds a route module into the store, which contains the state representing the current route:

    store.state.route.path   // current path (string)
    store.state.route.params // current params (object)
    store.state.route.query  // current query (object)
  • When the router navigates to a new route, the store's state is updated.

  • store.state.route is immutable, because it is derived state from the URL, which is the source of truth. You should not attempt to trigger navigations by mutating the route object. Instead, just call $router.push() or $router.go(). Note that you can do $router.push({ query: {...}}) to update the query string on the current path.

License

MIT