vuex-router-sync

Effortlessly keep vue-router and vuex store in sync.
4,091,575 2,487
Install
npm install vuex-router-sync
Latest Version:5.0.0
Older releases:2
License:MIT
Last Updated:Jan 8, 2022
Links: GitHub  ·  npm
Maintainer: yyx990803

Vuex Router Sync

npm ci status coverage license

Sync Vue Router's current $route as part of Vuex store's state.

中文版本 (Chinese Version)

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 './store' // vuex store instance
import router from './router' // vue-router instance

const unsync = sync(store, router) // done. Returns an unsync callback fn

// bootstrap your app...

// During app/Vue teardown (e.g., you only use Vue.js in a portion of your app
// and you navigate away from that portion and want to release/destroy
// Vue components/resources)
unsync() // Unsyncs store from router

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

Related Packages

vuexi

Vuex and Vue Router helper functions

642 5
spyfu-vue-factory

Unit testing factory for Vue components.

1,058 1
spyfu-vuex-helpers

Utility functions for Vuex.

2,459 9
web-killer

vue2 webcomponents

1,438
vue-fetch-route

This npm package provides various helper methods to ease the pain of fetching, s...

895 6

Version History

Version Released License
5.0.0 MIT
4.3.2 MIT
4.3.1 MIT
4.3.0 MIT
4.2.0 MIT
Pre-releases (1)
Version Released License
6.0.0-rc.1 MIT