1 | exports.sync = function (store, router) {
|
2 | patchStore(store)
|
3 | store.router = router
|
4 |
|
5 | var isTimeTraveling = false
|
6 | var currentPath
|
7 |
|
8 |
|
9 | store.watch(
|
10 | function (state) {
|
11 | return state.route
|
12 | },
|
13 | function (route) {
|
14 | if (route.path === currentPath) {
|
15 | return
|
16 | }
|
17 | isTimeTraveling = true
|
18 | currentPath = route.path
|
19 | router.go(route.path)
|
20 | },
|
21 | { deep: true, sync: true }
|
22 | )
|
23 |
|
24 |
|
25 | router.afterEach(function (transition) {
|
26 | if (isTimeTraveling) {
|
27 | isTimeTraveling = false
|
28 | return
|
29 | }
|
30 | var to = transition.to
|
31 | currentPath = to.path
|
32 | store.dispatch('router/ROUTE_CHANGED', {
|
33 | path: to.path,
|
34 | query: to.query,
|
35 | params: to.params
|
36 | })
|
37 | })
|
38 | }
|
39 |
|
40 | function patchStore (store) {
|
41 |
|
42 | var set = store._vm.constructor.parsers.path.setPath
|
43 | store._dispatching = true
|
44 | set(store._vm._data, 'route', {
|
45 | path: '',
|
46 | query: null,
|
47 | params: null
|
48 | })
|
49 | store._dispatching = false
|
50 |
|
51 | store.hotUpdate({
|
52 | modules: {
|
53 | route: {
|
54 | mutations: {
|
55 | 'router/ROUTE_CHANGED': function (state, to) {
|
56 | state.path = to.path
|
57 | state.query = to.query
|
58 | state.params = to.params
|
59 | }
|
60 | }
|
61 | }
|
62 | }
|
63 | })
|
64 | }
|