组件通信-父组件为子组件传递数据-静态数据
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<h1>{{rootMesage}}</h1>
<!-- 给子组件传递了一个字符串:'Hello World' -->
<child1-component message="Hello World" second-message="你好,世界" thirdMessage="第三条消息"></child1-component>
</div>
<script src="./vue.js"></script>
<script>
// HTML 标签的属性名是不区分大小写,即使你写的是大写的{例如,thirdMessage ),也会被作为小写的对待(例如 thirdmessage)。
// 如果出现了同名的属性,则只接受第一个属性的值,其他会被忽略。
// 如果传入的属性名有多个单词组成,则可以将多个单词以连接符(-)分隔,
// 子组件在接收参数时,需要将连接符去掉,然后将连接符后面的单首字母大写。
const app = new Vue({
el: '#app',
data: {
rootMesage: 'app根组件的数据'
},
components: {
'child1-component': {
props: ['message', 'secondMessage', 'thirdmessage'],
data() {
return {
child1Message: 'child1组件的数据'
}
},
template: `<div>
<h2>{{child1Message}}</h2>
<h2>{{message}}</h2>
<h2>{{secondMessage}}</h2>
<h2>{{thirdmessage}}</h2>
</div>`
}
}
});
// props properties 属性
// ref reference 引用
</script>
</body>
</html>
组件通信-父组件为子组件传递数据-动态数据
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<h1>{{rootMesage}}</h1>
<!-- 将父组件中的数据传递给子组件,例如 rootMessage -->
<child1-component :message="rootMesage"></child1-component>
</div>
<script src="./vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
rootMesage: 'app根组件的数据'
},
components: {
'child1-component': {
props: ['message'],
data() {
return {
child1Message: 'child1组件的数据'
}
},
template: `<div>
<h2>{{child1Message}}</h2>
<h2>{{message}}</h2>
</div>`
}
}
});
// props properties 属性
// ref reference 引用
</script>
</body>
</html>
数据校验
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="app">
<child1-component :root-name="name" :root-age="age" :root-gender="gender" :root-marry="marry"
:root-wechat="wechat" :root-hobby="hobby"></child1-component>
</div>
<script src="./vue.js"></script>
<script>
const app = new Vue({
el: '#app',
data: {
name: '苑文浩',
age: 20,
gender: true,
marry: '已婚',
wechat: '1271876231873',
hobby: '学习'
},
components: {
'child1-component': {
props: {
// 必须要传入,并且数据类型必须是字符串。
rootName: {
type: String,
required: true
},
// 可以不传递,但是,一旦传递,则数据类型必须是 Number 类型
rootAge: Number,
// 可以不传递,但是,一旦传递,则数据类型必须是 Boolean 或者 String 类型
rootGender: [Boolean, String],
// 可以不传递,默认是 '未婚',一旦传递,则数据类型必须是 String 类型
rootMarry: {
type: String,
default: '未婚'
},
// 可以不传递,默认是 '110',一旦传递,则数据类型必须是 String 类型
rootWechat: {
type: String,
default () {
return '110'
}
},
// 可以不传递,一旦传递,数据类型必须是字符串,并且,必须是数组中包含的值。
rootHobby: {
type: String,
// 自定义校验器,如果传入的数据合法,则返回 true,否则返回 false
validator(hobby) {
return ['吃饭', '睡觉', '学习'].includes(hobby)
}
}
},
data() {
return {
child1Message: 'child1组件的数据'
}
},
template: `<div>
<h2>{{child1Message}}</h2>
<h2>{{rootName}}</h2>
<h2>{{rootGender}}</h2>
<h2>{{rootMarry}}</h2>
<h2>{{rootWechat}}</h2>
<h2>{{rootHobby}}</h2>
</div>`
}
}
});
// props properties 属性
// ref reference 引用
</script>
</body>
</html>