-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path30.组件通信3——props类型验证.html
48 lines (47 loc) · 1.23 KB
/
30.组件通信3——props类型验证.html
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
<!--
prop类型验证
当prop 是一个对象而不是字符串数组时,它包含验证要求
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>prop类型验证 </title>
<script type="text/javascript"src="vue.js"></script>
</head>
<body>
</body>
<script type="text/javascript">
Vue.component('example', {
props: {
// 基础类型检测 (`null` 意思是任何类型都可以)
propA: Number,
// 多种类型
propB: [String, Number],
// 必传且是字符串
propC: {
type: String,
required: true
},
// 数字,有默认值
propD: {
type: Number,
default: 100
},
// 数组/对象的默认值应当由一个工厂函数返回
propE: {
type: Object,
default: function () {
return { message: 'hello' }
}
},
// 自定义验证函数
propF: {
validator: function (value) {
return value > 10
}
}
}
})
</script>
</html>