UNPKG

1.32 kBMarkdownView Raw
1# vuex-router-sync
2
3> Effortlessly keep vue-router and vuex store in sync.
4
5### Usage
6
7``` bash
8# the latest version works only with vue-router >= 2.0
9npm install vuex-router-sync
10
11# for usage with vue-router < 2.0:
12npm install vuex-router-sync@2
13```
14
15``` js
16import { sync } from 'vuex-router-sync'
17import store from './vuex/store' // vuex store instance
18import router from './router' // vue-router instance
19
20sync(store, router) // done.
21
22// bootstrap your app...
23```
24
25You can set a custom vuex module name
26
27```js
28sync(store, router, { moduleName: 'RouteModule' } )
29```
30
31
32### How does it work?
33
34- It adds a `route` module into the store, which contains the state representing the current route:
35
36 ``` js
37 store.state.route.path // current path (string)
38 store.state.route.params // current params (object)
39 store.state.route.query // current query (object)
40 ```
41
42- When the router navigates to a new route, the store's state is updated.
43
44- **`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.go()`. Note that you can do `$router.go({ query: {...}})` to update the query string on the current path.
45
46### License
47
48[MIT](http://opensource.org/licenses/MIT)
49
\No newline at end of file