验证支持的类型
String
Number
Boolean
Array
Object
Date
Function
Symbol
1、类型限制(单值)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="../js/vue.js"></script>
</head>
<body>
<div id ="app">
<!-- 使用父组件的数据时,必须使用v-bind指令 -->
<cpn :cmessage="message"></cpn>
</div>
<template id="cpn">
<div>
<h2>{{cmessage}}</h2>
</div>
</template>
<script>
// 父传子:props
const cpn={
template : '#cpn',
props:{
// 1,类型限制( 'null'匹配任何类型)
cmessage:String
}
}
//创建Vue实例,得到 ViewModel
var vm = new Vue({
el: '#app',
data:{
message: '好冷啊',
movides:['温暖的抱抱','拆弹专家','送你一朵红花']
},
components:{
cpn
}
});
</script>
</body>
</html>
2、类型限制(多个限制)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="../js/vue.js"></script>
</head>
<body>
<div id ="app">
<!-- 使用父组件的数据时,必须使用v-bind指令 -->
<cpn :cmessage="message"></cpn>
</div>
<template id="cpn">
<div>
<h2>{{cmessage}}</h2>
</div>
</template>
<script>
// 父传子:props
const cpn={
template : '#cpn',
props:{
// 2,类型限制( 'null'匹配任何类型)
cmessage:[String,Number]
}
}
//创建Vue实例,得到 ViewModel
var vm = new Vue({
el: '#app',
data:{
message: '好冷啊',
movides:['温暖的抱抱','拆弹专家','送你一朵红花']
},
components:{
cpn
}
});
</script>
</body>
</html>
3、required
使用required必须使用v-bind绑定相应的值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="../js/vue.js"></script>
</head>
<body>
<div id ="app">
<!-- 使用父组件的数据时,必须使用v-bind指令 -->
<cpn></cpn>
</div>
<template id="cpn">
<div>
<h2>{{cmessage}}</h2>
<h2>{{cmovides}}</h2>
</div>
</template>
<script>
// 父传子:props
const cpn={
template : '#cpn',
props:{
cmessage:{
type: String,
required: true
},
cmovides:{
type: Array
}
}
}
//创建Vue实例,得到 ViewModel
var vm = new Vue({
el: '#app',
data:{
message: '好冷啊',
movides:['温暖的抱抱','拆弹专家','送你一朵红花']
},
components:{
cpn
}
});
</script>
</body>
</html>
4、带有默认值的字符串
使用默认值,则不需要使用v-bind绑定。如果使用了v-bind绑定,则默认值无效。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="../js/vue.js"></script>
</head>
<body>
<div id ="app">
<!-- 使用父组件的数据时,必须使用v-bind指令 -->
<cpn></cpn>
</div>
<template id="cpn">
<div>
<h2>{{cmessage}}</h2>
</div>
</template>
<script>
// 父传子:props
const cpn={
template : '#cpn',
props:{
cmessage:{
type: String,
default : '踏山河'
}
}
}
//创建Vue实例,得到 ViewModel
var vm = new Vue({
el: '#app',
data:{
message: '好冷啊',
movides:['温暖的抱抱','拆弹专家','送你一朵红花']
},
components:{
cpn
}
});
</script>
</body>
</html>
5、带有默认值的数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="../js/vue.js"></script>
</head>
<body>
<div id ="app">
<!-- 使用父组件的数据时,必须使用v-bind指令 -->
<cpn></cpn>
</div>
<template id="cpn">
<div>
<h2>{{cmessage}}</h2>
</div>
</template>
<script>
// 父传子:props
const cpn={
template : '#cpn',
props:{
// 对象或数组默认值必须从一个工厂函数获取
cmessage:{
type: Array,
default(){
return ['真冷','生死无话']
}
}
}
}
//创建Vue实例,得到 ViewModel
var vm = new Vue({
el: '#app',
data:{
message: '好冷啊',
movides:['温暖的抱抱','拆弹专家','送你一朵红花']
},
components:{
cpn
}
});
</script>
</body>
</html>
6、使用validator
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="../js/vue.js"></script>
</head>
<body>
<div id ="app">
<!-- 使用父组件的数据时,必须使用v-bind指令 -->
<cpn :cmessage="message"></cpn>
</div>
<template id="cpn">
<div>
<h2>{{cmessage}}</h2>
</div>
</template>
<script>
// 父传子:props
const cpn={
template : '#cpn',
props:{
cmessage:{
// cmessage值必须匹配下列字符串中的一个,否则会报错
validator(value){
return ['success','warn'].indexOf(value)!==-1;
}
}
}
}
//创建Vue实例,得到 ViewModel
var vm = new Vue({
el: '#app',
data:{
message: 'warn',
movides:['温暖的抱抱','拆弹专家','送你一朵红花']
},
components:{
cpn
}
});
</script>
</body>
</html>