<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>组件参数校验与非props的特性</title>
<script src="../js/vue.js"></script>
</head>
<body>
<div id="root">
<child content="hell"></child>
</div>
</body>
<script>
Vue.component('child', {
props: {
content: {
type: Object,
//表示content必传
// required:true,
//设置默认值 没有content传过来
// default:'default value',
// 检验参数
validator: function(value) {
return value.length > 5
}
}
},
template: '<div>{{content}}</div>'
})
var vm = new Vue({
el: "#root"
})
</script>
</html>
转载
原文链接:https://blog.csdn.net/weixin_36934930/article/details/80218601
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: {
type: String,
validator: function (t) {
// 这个值必须匹配下列字符串中的一个
return t === ‘fade’ || t === ‘slide’
},
defalut:‘slide’
}