|
1 | 1 | exports.sync = function (store, router) {
|
2 |
| - patchStore(store) |
3 |
| - store.router = router |
| 2 | + store.registerModule('route', { |
| 3 | + state: {}, |
| 4 | + mutations: { |
| 5 | + 'router/ROUTE_CHANGED': function (state, to) { |
| 6 | + store.state.route = Object.freeze({ |
| 7 | + name: to.name, |
| 8 | + path: to.path, |
| 9 | + hash: to.hash, |
| 10 | + query: to.query, |
| 11 | + params: to.params, |
| 12 | + fullPath: to.fullPath |
| 13 | + }) |
| 14 | + } |
| 15 | + } |
| 16 | + }) |
4 | 17 |
|
5 |
| - var commit = store.commit || store.dispatch |
6 | 18 | var isTimeTraveling = false
|
7 | 19 | var currentPath
|
8 | 20 |
|
9 | 21 | // sync router on store change
|
10 | 22 | store.watch(
|
11 |
| - function (state) { |
12 |
| - return state.route |
13 |
| - }, |
| 23 | + function (state) { return state.route }, |
14 | 24 | function (route) {
|
15 |
| - if (route.path === currentPath) { |
| 25 | + if (route.fullPath === currentPath) { |
16 | 26 | return
|
17 | 27 | }
|
18 | 28 | isTimeTraveling = true
|
19 |
| - currentPath = route.path |
20 |
| - router.go(route.path) |
| 29 | + currentPath = route.fullPath |
| 30 | + router.push(route) |
21 | 31 | },
|
22 |
| - { deep: true, sync: true } |
| 32 | + { sync: true } |
23 | 33 | )
|
24 | 34 |
|
25 | 35 | // sync store on router navigation
|
26 |
| - router.afterEach(function (transition) { |
| 36 | + router.afterEach(function (to) { |
27 | 37 | if (isTimeTraveling) {
|
28 | 38 | isTimeTraveling = false
|
29 | 39 | return
|
30 | 40 | }
|
31 |
| - var to = transition.to |
32 |
| - currentPath = to.path |
33 |
| - commit('router/ROUTE_CHANGED', to) |
34 |
| - }) |
35 |
| -} |
36 |
| - |
37 |
| -function applyMutationState(store, state) { |
38 |
| - // support above 2.0 |
39 |
| - if (store.hasOwnProperty('_committing')) { |
40 |
| - return store._committing = state |
41 |
| - } |
42 |
| - return store._dispatching = state |
43 |
| -} |
44 |
| - |
45 |
| -function patchStore (store) { |
46 |
| - // add state |
47 |
| - var set = store._vm.constructor.set |
48 |
| - applyMutationState(store, true); |
49 |
| - set(store.state, 'route', { |
50 |
| - path: '', |
51 |
| - query: null, |
52 |
| - params: null |
| 41 | + currentPath = to.fullPath |
| 42 | + store.commit('router/ROUTE_CHANGED', to) |
53 | 43 | })
|
54 |
| - applyMutationState(store, false); |
55 |
| - |
56 |
| - var routeModule = { |
57 |
| - mutations: { |
58 |
| - 'router/ROUTE_CHANGED': function (state, to) { |
59 |
| - store.state.route = to |
60 |
| - } |
61 |
| - } |
62 |
| - } |
63 |
| - |
64 |
| - // add module |
65 |
| - if (store.module) { |
66 |
| - store.module('route', routeModule) |
67 |
| - } else { |
68 |
| - store.hotUpdate({ |
69 |
| - modules: { |
70 |
| - route: routeModule |
71 |
| - } |
72 |
| - }) |
73 |
| - } |
74 | 44 | }
|
0 commit comments