如果传入的数据不符合规格, Vue (开发版本)会产生控制台警告。
<template>
<div>
<example :propA="'hello'" :propB="{hello:'hello'}" :propC="''" :propD="'hello'" :propE="{hello:'hello'}" :propF="10"></example>
</div>
</template>
Vue.component('example', {
props: {
propA: Number, // 数字类型 (`null` 意思是任何类型都可以)
propB: [ String, Number ], // 多种类型:字符串、数字类型
propC: {
type: String, required: true }, // 字符串、必传
propD: {
type: Number, default: 100 }, // 数字,有默认值
// 数组、对象的默认值应当由一个工厂函数返回
propE: {
type: Object,
default: function () {
return {
message: 'hello1' }
}
},
// 自定义验证函数
propF: {
validator: function (value) {
return value > 10
}
}
}
})
注意:定验证规格需要用对象的形式,不能用字符串数组