<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>组件参数校验与非props特性</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="root">
<child content="helloa"></child>
</div>
<script>
Vue.component('child',{
props:{
content:{
type: String,
/*required: false,
default:'default value' //若不传,则为默认值
validator:function(value){
return(value.length > 5)
}*/
}
},
template:'<div>{{content}}</div>'
})
var vm = new Vue({
el:'#root'
})
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>组件参数校验与非props特性</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="root">
<child content="helloa"></child>
</div>
<script>
Vue.component('child',{
props:{
content:{
type: String,
/*required: false,
default:'default value' //若不传,则为默认值
validator:function(value){
return(value.length > 5)
}*/
}
},
template:'<div>{{content}}</div>'
})
var vm = new Vue({
el:'#root'
})
</script>
</body>
</html>
本文通过一个简单的Vue组件示例介绍了如何进行组件参数校验,并展示了如何使用默认值及验证函数确保传递给组件的属性符合预期。该示例还包含了基本的HTML结构和Vue.js脚本。
418

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



