<!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>

本文探讨了Vue.js中组件参数的多种校验方式,包括类型检查、必填项验证及自定义验证函数,并介绍了非props特性的使用场景,即如何在子组件的DOM属性中展示非props传递的数据。
1363

被折叠的 条评论
为什么被折叠?



