vuex-router-sync
Sync vue-router's current $route as part of vuex store's state.
Usage
npm install vuex-router-sync
npm install vuex-router-sync@2
import { sync } from 'vuex-router-sync'
import store from './vuex/store'
import router from './router'
const unsync = sync(store, router)
unsync()
You can optionally 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
store.state.route.params
store.state.route.query
-
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