vuex-router-sync

Effortlessly keep vue-router and vuex store in sync.

Github stars Tracking Chart

vuex-router-sync CircleCI

Sync vue-router's current $route as part of vuex store's state.

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

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

Main metrics

Overview
Name With Ownervuejs/vuex-router-sync
Primary LanguageJavaScript
Program languageJavaScript (Language Count: 2)
Platform
License:MIT License
所有者活动
Created At2016-03-08 10:32:28
Pushed At2022-01-08 14:55:22
Last Commit At2021-02-05 10:03:23
Release Count6
Last Release Namev6.0.0-rc.1 (Posted on )
First Release Namev2.0.0 (Posted on 2016-06-30 16:34:06)
用户参与
Stargazers Count2.5k
Watchers Count47
Fork Count123
Commits Count79
Has Issues Enabled
Issues Count73
Issue Open Count8
Pull Requests Count20
Pull Requests Open Count3
Pull Requests Close Count11
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private