forked from vuejs/vue-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
142 lines (126 loc) · 3.69 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import Vue from 'vue'
import VueRouter from 'vue-router'
import Post from '../data-fetching/Post.vue'
Vue.use(VueRouter)
const Home = { template: '<div>home</div>' }
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
function guardRoute (to, from, next) {
if (window.confirm(`Navigate to ${to.path}?`)) {
next()
} else if (window.confirm(`Redirect to /baz?`)) {
next('/baz')
} else {
next(false)
}
}
// Baz implements an in-component beforeRouteLeave hook
const Baz = {
data () {
return { saved: false }
},
template: `
<div>
<p>baz ({{ saved ? 'saved' : 'not saved' }})</p>
<button @click="saved = true">save</button>
</div>
`,
beforeRouteLeave (to, from, next) {
if (this.saved || window.confirm('Not saved, are you sure you want to navigate away?')) {
next()
} else {
next(false)
}
}
}
// Baz implements an in-component beforeRouteEnter hook
const Qux = {
data () {
return {
msg: null
}
},
template: `<div>{{ msg }}</div>`,
beforeRouteEnter (to, from, next) {
// Note that enter hooks do not have access to `this`
// because it is called before the component is even created.
// However, we can provide a callback to `next` which will
// receive the vm instance when the route has been confirmed.
//
// simulate an async data fetch.
// this pattern is useful when you want to stay at current route
// and only switch after the data has been fetched.
setTimeout(() => {
next(vm => {
vm.msg = 'Qux'
})
}, 300)
}
}
// Quux implements an in-component beforeRouteUpdate hook.
// this hook is called when the component is reused, but the route is updated.
// For example, when navigating from /quux/1 to /quux/2.
const Quux = {
data () {
return {
prevId: 0
}
},
template: `<div>id:{{ $route.params.id }} prevId:{{ prevId }}</div>`,
beforeRouteUpdate (to, from, next) {
this.prevId = from.params.id
next()
}
}
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{ path: '/', component: Home },
// post with transitions
{ path: '/post/:id', component: Post },
// inline guard
{ path: '/foo', component: Foo, beforeEnter: guardRoute },
// using meta properties on the route config
// and check them in a global before hook
{ path: '/bar', component: Bar, meta: { needGuard: true }},
// Baz implements an in-component beforeRouteLeave hook
{ path: '/baz', component: Baz },
// Qux implements an in-component beforeRouteEnter hook
{ path: '/qux', component: Qux },
// in-component beforeRouteEnter hook for async components
{ path: '/qux-async', component: resolve => {
setTimeout(() => {
resolve(Qux)
}, 0)
} },
// in-component beforeRouteUpdate hook
{ path: '/quux/:id', component: Quux }
]
})
router.beforeEach((to, from, next) => {
if (to.matched.some(m => m.meta.needGuard)) {
guardRoute(to, from, next)
} else {
next()
}
})
new Vue({
router,
template: `
<div id="app">
<h1>reload() behaviors</h1>
<button @click="$router.reload()">$router.reload()</button>
<ul>
<li><router-link to="/">/</router-link></li>
<li><router-link to="/post/1">/post/1</router-link></li>
<li><router-link to="/foo">/foo</router-link></li>
<li><router-link to="/bar">/bar</router-link></li>
<li><router-link to="/baz">/baz</router-link></li>
<li><router-link to="/quux/1">/quux/1</router-link></li>
<li><router-link to="/quux/2">/quux/2</router-link></li>
</ul>
<router-view class="view"></router-view>
</div>
`
}).$mount('#app')