Vue父子組件之間的屬性訪問(wèn)之children,refs,parent,root方法

父訪問(wèn)子:

<!DOCTYPE?html>

<html?lang="en">

<head>

??<meta?charset="UTF-8">

??<meta?name="viewport"?content="width=device-width,?initial-scale=1.0">

??<title>Document</title>

</head>

<body>

??<div?id="app">

????<cpn></cpn>

????<cpn?ref="aaa"></cpn>

????<cpn></cpn>

????<button?@click="btnClick">按鈕</button>

??</div>

??<template?id="cpn">

????<div>

??????<h2>我是子組件</h2>

????</div>

??</template>

??<script?src="../../js/vue.js"></script>

??<script>

????const?app??=?new?Vue({

??????el:?"#app",

??????data:?{

??????},

??????components:{

????????cpn:?{

??????????template:?'#cpn',

??????????data()?{

????????????return?{

??????????????name:?"我是子組件的name"

????????????}

??????????},

??????????methods:?{

????????????showMessage()?{

??????????????console.log('我是子組件')

????????????}

??????????},

????????}

??????},

??????methods:?{

????????btnClick()?{

??????????//?父訪問(wèn)子方法:$children(一般不使用這種方法,因?yàn)榻M件數(shù)量在變化,用下標(biāo)容易發(fā)生混亂)

??????????//?console.log(this.$children),

??????????//?this.$children[0].showMessage()

??????????//?for(item?of?this.$children){

??????????//???console.log(item.showMessage())

??????????//?}

??????????//?$refs,需在使用父組件時(shí)定義其鍵名,一般使用這個(gè)

??????????console.log(this.$refs.aaa.name,this.$refs.aaa.showMessage());

??????????//?console.log(this.$refs.aaa.showMessage())

????????}

??????},

????})

??</script>

</body>

</html>

子訪問(wèn)父

<!DOCTYPE?html>

<html?lang="en">

<head>

??<meta?charset="UTF-8">

??<meta?name="viewport"?content="width=device-width,?initial-scale=1.0">

??<title>Document</title>

</head>

<body>

??<div?id="app">

????<cpn></cpn>

??</div>

??<template?id="cpn">

????<div>

??????<h2>我是子組件</h2>

??????<ccpn></ccpn>

????</div>

??</template>

??<template?id="ccpn">

????<div>

??????<h2>我是孫組件</h2>

??????<button?@click="btnClick">按鈕</button>

????</div>

??</template>

??<script?src="../../js/vue.js"></script>

??<script>

????const?app??=?new?Vue({

??????el:?"#app",

??????data:?{

????????name:?"我是頂層的name"

??????},

??????components:{

????????cpn:?{

??????????template:?'#cpn',

??????????data()?{

????????????return?{

??????????????name:?"我是子組件的name"

????????????}

??????????},


??????????components:{

????????????ccpn:?{

??????????????template:?'#ccpn',

??????????????methods:?{

????????????????btnClick()?{

??????????????????//?$parent訪問(wèn)的是組件上一層的屬性

??????????????????console.log(this.$parent.name),

??????????????????//?$root訪問(wèn)的是頂層組件的屬性,兩種方法都很少用,因?yàn)橐苊怦詈?/p>

??????????????????console.log(this.$root.name)

????????????????}???????

??????????????},

????????????}

??????????}

????????}

??????},


????})

??</script>

</body>

</html>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容