forked from vuejs/vue-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
171 lines (158 loc) · 3.94 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const Wrap = { template: '<div>child: <router-view></router-view></div>' }
const Index = {
template: '<wrap />',
components: {
Wrap
}
}
const WithGuard = {
template: '<div>{{ $route.name }}: {{ n }}</div>',
data: () => ({ n: 0 }),
beforeRouteEnter (to, from, next) {
next(vm => {
vm.n++
})
}
}
const IndexChild1 = { template: '<div>index child1</div>' }
const IndexChild2 = { template: '<div>index child2</div>' }
const Home = { template: '<div>home</div>' }
const ViewWithKeepalive = {
template: '<keep-alive><router-view></router-view></keep-alive>'
}
const Parent = { template: '<div>msg: {{ msg }}<router-view /></div>', props: ['msg'] }
const RequiredProps = {
template: '<div>props from route config is: {{ msg }}</div>',
props: {
msg: {
type: String,
required: true
}
}
}
// keep original values to restore them later
const originalSilent = Vue.config.silent
const originalWarnHandler = Vue.config.warnHandler
const CatchWarn = {
template: `<div>{{ didWarn ? 'caught missing prop warn' : 'no missing prop warn' }}</div>`,
data () {
return {
didWarn: false
}
},
beforeRouteEnter (to, from, next) {
let missPropWarn = false
Vue.config.silent = false
Vue.config.warnHandler = function (msg, vm, trace) {
if (/Missing required prop/i.test(msg)) {
missPropWarn = true
}
}
next(vm => {
vm.didWarn = missPropWarn
})
},
beforeRouteLeave (to, from, next) {
// restore vue config
Vue.config.silent = originalSilent
Vue.config.warnHandler = originalWarnHandler
next()
}
}
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{
path: '/index',
component: Index,
children: [
{
path: 'child1',
component: IndexChild1
},
{
path: 'child2',
component: IndexChild2
}
]
},
{
path: '/home',
component: Home
},
{
path: '/with-guard1',
name: 'with-guard1',
component: WithGuard
},
{
path: '/with-guard2',
name: 'with-guard2',
component: WithGuard
},
{
path: '/one',
component: ViewWithKeepalive,
children: [
{
path: 'two',
component: ViewWithKeepalive,
children: [
{
path: 'child1',
component: IndexChild1
},
{
path: 'child2',
component: IndexChild2
}
]
}
]
},
{
path: '/config-required-props',
component: Parent,
props: { msg: 'from parent' },
children: [
{
path: 'child',
component: RequiredProps,
props: {
msg: 'from child'
}
}
]
},
{
path: '/catch-warn',
component: CatchWarn
}
]
})
new Vue({
router,
template: `
<div id="app">
<ul>
<li><router-link to="/index/child1">/index/child1</router-link></li>
<li><router-link to="/index/child2">/index/child2</router-link></li>
<li><router-link to="/home">/home</router-link></li>
<li><router-link to="/with-guard1">/with-guard1</router-link></li>
<li><router-link to="/with-guard2">/with-guard2</router-link></li>
<li><router-link to="/one/two/child1">/one/two/child1</router-link></li>
<li><router-link to="/one/two/child2">/one/two/child2</router-link></li>
<li><router-link to="/config-required-props">/config-required-props</router-link></li>
<li><router-link to="/config-required-props/child">/config-required-props/child</router-link></li>
<li><router-link to="/catch-warn">/catch-warn</router-link></li>
</ul>
<keep-alive>
<router-view class="view"></router-view>
</keep-alive>
</div>
`
}).$mount('#app')