
批改狀態(tài):合格
老師批語(yǔ):
1.父級(jí)組件通過v-bind把數(shù)據(jù)綁定到子組件的標(biāo)簽屬性中。
2.自己組件通過實(shí)列props屬性來接收父級(jí)傳過來的數(shù)據(jù)即可
3.代碼
<!DOCTYPE html>
<html lang="zh">
<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>組件間數(shù)據(jù)的傳遞</title>
<script src="vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="app">
<label for="num1">第一個(gè)數(shù)據(jù):</label><input type="text" id="num1" v-model.number="num1">
<label for="num2">第二個(gè)數(shù)據(jù):</label><input type="text" id="num2" v-model.number="num2">
<sum :sum="sum"></sum>
</div>
</body>
<script>
const vm=new Vue({
el:".app",
data:{
num1:1,
num2:2,
},
computed:{
sum(){
return this.num1+this.num2;
}
},
components:{
sum:{
props:["sum"],
template:`<h4>{{sum}}</h4>`,
}
}
})
</script>
</html>
4.演示結(jié)果
1.子組件的掛載點(diǎn):html內(nèi)容必須有一個(gè)標(biāo)簽包裹
2.子組件傳數(shù)據(jù)
4、演示結(jié)果
<!DOCTYPE html>
<html lang="zh">
<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>組件間數(shù)據(jù)的傳遞</title>
<script src="vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="app">
<span-sum @get="getsum"></span-sum>
<h1>{{sum}}</h1>
</div>
<template id="son">
<div>
<label for="num1">第一個(gè)數(shù)據(jù):</label><input type="text" id="num1" v-model.number="num1">
<label for="num2">第二個(gè)數(shù)據(jù):</label><input type="text" id="num2" v-model.number="num2">
<button type="button" @click="getsum()">計(jì)算</button>
<span>{{sum}}</span>
</div>
</template>
</body>
<script>
// 子組件
const spanSum={
data(){
return {
num1:1,
num2:2,
sum:3,
}
},
template:"#son",
methods:{
getsum(){
this.sum=this.num1 + this.num2;
this.$emit('get',this.sum);
}
}
}
// 父組件
const vm=new Vue({
el:".app",
data:{
sum:"答案",
},
components:{
spanSum,
},
methods:{
getsum(sum){
console.log(sum);
this.sum=sum;
}
}
})
</script>
</html>
1.組件實(shí)際上就是一個(gè)Vue實(shí)例,只不過時(shí)通過自定義標(biāo)簽名隱式掛載的
2.創(chuàng)建組件關(guān)鍵字:Vue.extend({})
3.注冊(cè)全局組件:Vue.component(“自定義標(biāo)簽名”,組件)
4.組件的數(shù)據(jù)data,必須是一個(gè)函數(shù)的return返回值
5.組件可以重復(fù)利用,且是一個(gè)封閉的對(duì)象(避免污染全局)
6.一般不建議創(chuàng)建全局組件
7.全局組件,可以載任何vue實(shí)例模板中直接使用
8.代碼
<!DOCTYPE html>
<html lang="zh">
<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>組件的組成和分類</title>
<script src="vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="app">
{{leave}}
<child-div></child-div>
<child-div></child-div>
<!-- <child></child>
<child></child>
<child></child> -->
</div>
<template id="child">
<span>子組件</span>
</template>
</body>
<script type="text/javascript">
// 創(chuàng)建組件
const child=Vue.extend(
{
template:"#child",
}
);
// 注冊(cè)全局組件
Vue.component("childDiv",child);
const vm=new Vue({
el:".app",
data(){
return {
site:"種業(yè)圈",
leave:"父級(jí)組件",
}
},
// 局部組件
components:{
// child,
}
})
</script>
</html
9.運(yùn)行結(jié)果
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)