-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathroute.js
46 lines (43 loc) · 1.14 KB
/
route.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
const internalKeysRE = /^(component|subRoutes|fullPath)$/
/**
* Route Context Object
*
* @param {String} path
* @param {Router} router
*/
export default class Route {
constructor (path, router) {
const matched = router._recognizer.recognize(path)
if (matched) {
// copy all custom fields from route configs
[].forEach.call(matched, match => {
for (let key in match.handler) {
if (!internalKeysRE.test(key)) {
this[key] = match.handler[key]
}
}
})
// set query and params
this.query = matched.queryParams
this.params = [].reduce.call(matched, (prev, cur) => {
if (cur.params) {
for (let key in cur.params) {
prev[key] = cur.params[key]
}
}
return prev
}, {})
}
// expose path and router
this.path = path
// for internal use
this.matched = matched || router._notFoundHandler
// internal reference to router
Object.defineProperty(this, 'router', {
enumerable: false,
value: router
})
// Important: freeze self to prevent observation
Object.freeze(this)
}
}