<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>组件参数校验与非Props特性</title>
</head>
<body>
<div id="app">
<child content="helloworld"></child>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
Vue.component('child',{
props: {
// content: String, //组件参数类型校验
// content: Number, //组件参数类型校验
// content: [Number,String], //组件参数类型校验
// content: {
// type: String,
// required: true
// },
// content: {
// type: String,
// required: false,
// default: 'defaultValue'
// },
content: {
type: String,
validator: function (value) {
return value.length < 5;
}
}
},
template: '<div>{{content}}</div>'
});
var app = new Vue({
el: '#app'
});
</script>
</html>
非props特性:非props特性将展示在子组件的dom属性里
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>组件参数校验与非Props特性</title>
</head>
<body>
<div id="app">
<child content="haha"></child>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
Vue.component('child',{
template: '<div>hello world</div>'
});
var app = new Vue({
el: '#app'
});
</script>
</html>
来源:https://blog.csdn.net/baokx/article/details/99080683