-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathindex.ts
258 lines (225 loc) · 5.12 KB
/
index.ts
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import Vue, { ComponentOptions, AsyncComponent, Component } from 'vue'
import VueRouter from '../index'
import {
Route,
RouteRecord,
RedirectOption,
NavigationFailure,
NavigationFailureType
} from '../index'
Vue.use(VueRouter)
const Home = { template: '<div>home</div>' }
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
const Abc = { template: '<div>abc</div>' }
const Async = () => Promise.resolve({ template: '<div>async</div>' })
let err: any
if (
VueRouter.isNavigationFailure(err, VueRouter.NavigationFailureType.aborted)
) {
err.from.fullPath.split('/')
}
let navigationFailure = new Error() as NavigationFailure
navigationFailure.to.fullPath.split('/')
const Hook: ComponentOptions<Vue> = {
template: '<div>hook</div>',
beforeRouteEnter(to, from, next) {
route.params
next('/')
next({ path: '/' })
next(vm => {
vm.$router
})
},
beforeRouteLeave(to, from, next) {
route.params
next('/')
next({ path: '/' })
next()
},
beforeRouteUpdate(to, from, next) {
route.params
next('/')
next({ path: '/' })
next()
}
}
const JSXComponent = () => {
$props: {
}
}
const router = new VueRouter({
mode: 'history',
base: '/',
fallback: false,
linkActiveClass: 'active',
linkExactActiveClass: 'exact-active',
scrollBehavior: (to, from, savedPosition) => {
if (from.path === '/') {
return { selector: '#app' }
}
if (from.path === '/offset') {
return { selector: '#foo', offset: { x: 0, y: 100 } }
}
if (to.path === '/child') {
return
}
if (savedPosition) {
return savedPosition
}
return Promise.resolve({
x: 0,
y: 0
})
},
routes: [
{
path: '/foo',
component: Home,
children: [{ path: '', component: Home }]
},
{
path: '/foo',
components: { default: Home },
children: [{ path: '', component: Home }]
},
{
path: '/',
name: 'home',
component: Home,
children: [
{
path: 'child',
components: {
default: Foo,
bar: Bar,
abc: Abc,
asyncComponent: Async,
JSXComponent
},
meta: { auth: true, nested: { foo: '' } },
beforeEnter(to, from, next) {
to.params
from.params
next({ name: 'home' })
next()
},
props: {
default: true,
bar: { id: 123 },
abc: route => route.params,
asyncComponent: (route: Route) => route.params
}
},
{
path: 'children',
redirect: to => {
to.params
return '/child'
}
}
]
},
{ path: '/home', alias: '/' },
{ path: '/foo', props: true },
{ path: '/bar', props: { id: 123 } },
{ path: '/baz', props: (route: Route) => route.params },
{ path: '*', redirect: '/' }
]
})
const App: Vue = router.app
const mode: string = router.mode
const route: Route = router.currentRoute
const path: string = route.path
const name: string | undefined | null = route.name
const hash: string = route.hash
const query: string | (string | null)[] | null = route.query['foo']
const params: string = route.params['bar']
const fullPath: string = route.fullPath
const redirectedFrom: string | undefined = route.redirectedFrom
const meta: any = route.meta
const matched: RouteRecord[] = route.matched
matched.forEach(m => {
const path: string = m.path
const components: {
[key: string]: Component | AsyncComponent | {}
} = m.components
const instances: { [key: string]: Vue } = m.instances
const name: string | undefined | null = m.name
const parent: RouteRecord | undefined = m.parent
const redirect: RedirectOption | undefined = m.redirect
})
const unregister = router.beforeEach((to, from, next) => {
to.params
next('/')
next()
})
unregister()
router.beforeResolve((to, from, next) => {
to.params
from.params
next()
})
router.afterEach((to, from) => {
to.params
from.params
})
router.push({
path: '/',
params: {
foo: 'foo'
},
query: {
bar: 'bar',
empty: null,
removed: undefined,
withEmpty: ['1', null],
foo: ['foo1', 'foo2']
},
hash: 'hash'
})
router.replace({ name: 'home' })
router.push(
'/',
() => {},
() => {}
)
router.replace(
'/foo',
() => {},
() => {}
)
// promises
router
.push('/')
.then(route => {
route.fullPath
})
.catch(() => {})
router.onReady(() => {})
router.addRoutes([{ path: '/more' }])
router.go(-1)
router.back()
router.forward()
const Components: (
| Component
| AsyncComponent
| {}
)[] = router.getMatchedComponents()
const match: Route = router.match('/more')
const vm = new Vue({
router,
template: `
<div id="app">
<h1>Basic</h1>
<ul>
<li><router-link to="/">/</router-link></li>
<li><router-link to="/foo">/foo</router-link></li>
<li><router-link to="/bar">/bar</router-link></li>
</ul>
<router-view class="view"></router-view>
</div>
`
}).$mount('#app')
vm.$router.push('/')
vm.$route.params