<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://unpkg.com/vue@next"></script>
</head>
<body>
<div id="root"></div>
<script>
const HelloWorld = {
props: {
title: {
type: Number,
required: true,
validator: function(value) {
if(value < 10) {
throw new Error('值应该大于等于10')
}
return value
},
default: 0
},
contentAbc: Number // 父组件短横线传递,子组件驼峰命名接收
},
inheritAttrs: false, // 不接受父组件传来的non-props属性(不渲染非props接收的属性,比如样式等)
// $attrs: 接收父组件传来的所有non-props属性, 与inheritAttrs相当于对立的
template: `<div>
{{title}} {{contentAbc}}
<p v-bind="$attrs">$attrs:{{$attrs}}</p>
<p>{{$attrs.name}}</p>
</div>`
}
const app = Vue.createApp({
components: { HelloWorld }, // 局部组件注册 建议大写字母开头、驼峰命名
// Non-Props: 在父组件中传值,但子组件中没有在props中接收; 比如这里的name属性
template: `<div>
<hello-world :title="19" :content-abc="123" name="hello-world" style="color:red"/>
</div>`,
data(){
return {
msg: 'hello world',
}
},
methods: {
},
})
app.mount('#root')
</script>
</body>
</html>
vue3基础入门三-组件校验传值
最新推荐文章于 2024-07-11 11:12:10 发布