-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathprops.js
46 lines (39 loc) · 1.08 KB
/
props.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
import { warn } from './warn'
export function resolveProps (route, component, config) {
switch (typeof config) {
case 'undefined':
return
case 'object':
return config
case 'function':
return config(route)
case 'boolean':
if (!config) {
return
}
if (!component.props) {
return
}
const props = {}
for (const prop in route.params) {
const value = route.params[prop]
if (hasProp(component, prop)) {
const propType = component.props[prop].type
if (propType === null || propType === String) {
props[prop] = value
} else if (propType === Number && value.match(/^-?([0-9]+|[0-9]*\.[0-9]+)$/)) {
props[prop] = parseFloat(value)
}
}
}
return props
default:
warn(false, `props in "${route.path}" is a ${typeof config}, expecting an object, function or boolean.`)
}
}
function hasProp (component, prop) {
if (!component.props) {
return false
}
return typeof component.props[prop] !== 'undefined'
}