Vue組件實(shí)例間的直接訪問(wèn)實(shí)現(xiàn)代碼
前面的話
有時(shí)候需要父組件訪問(wèn)子組件,子組件訪問(wèn)父組件,或者是子組件訪問(wèn)根組件。 在組件實(shí)例中,Vue提供了相應(yīng)的屬性,包括$parent、$children、$refs和$root,這些屬性都掛載在組件的this上。本文將詳細(xì)介紹Vue組件實(shí)例間的直接訪問(wèn)
$parent
$parent表示父組件的實(shí)例,該屬性只讀
下面是一個(gè)簡(jiǎn)易實(shí)例
<div id="example"> <parent-component></parent-component> </div> <template id="parent-component"> <div class="parent"> <h3>我是父組件</h3> <input v-model="parentMsg"> <p>{{parentMsg}}</p> <child-component></child-component> </div> </template> <template id="child-component"> <div class="child"> <h3>我是子組件</h3> <p>{{msg}}</p> <button v-on:click="showData">顯示父組件數(shù)據(jù)</button> </div> </template>
<script> // 注冊(cè) Vue.component('parent-component', { template: '#parent-component', data(){ return{ parentMsg:'我是父組件的數(shù)據(jù)' } }, components:{ 'child-component':{ template:'#child-component', data(){ return{ msg:'' } }, methods:{ showData(){ this.msg = this.$parent.parentMsg; } } } } }) // 創(chuàng)建根實(shí)例 new Vue({ el: '#example' }) </script>
$root
$root表示當(dāng)前組件樹的根 Vue 實(shí)例。如果當(dāng)前實(shí)例沒(méi)有父實(shí)例,此實(shí)例將會(huì)是其自己。該屬性只讀
<div id="example"> <h3>我是根組件</h3> <input v-model="rootMsg"> <p>{{rootMsg}}</p> <parent-component></parent-component> </div> <template id="parent-component"> <div class="parent"> <h3>我是父組件</h3> <input v-model="parentMsg"> <p>{{parentMsg}}</p> <child-component></child-component> </div> </template> <template id="child-component"> <div class="child"> <h3>我是子組件</h3> <p> <button v-on:click="showRootData">顯示根組件數(shù)據(jù)</button><span>{{rootMsg}}</span> </p> <p> <button v-on:click="showParentData">顯示父組件數(shù)據(jù)</button><span>{{parentMsg}}</span> </p> </div> </template>
<script> // 注冊(cè) Vue.component('parent-component', { template: '#parent-component', data(){ return{ parentMsg:'我是父組件的數(shù)據(jù)' } }, components:{ 'child-component':{ template:'#child-component', data(){ return{ parentMsg:'', rootMsg:'' } }, methods:{ showParentData(){ this.parentMsg = this.$parent.parentMsg; }, showRootData(){ this.rootMsg = this.$root.rootMsg; }, } } } }) // 創(chuàng)建根實(shí)例 new Vue({ el: '#example', data:{ rootMsg:'我是根組件數(shù)據(jù)' } }) </script>
$children
$children表示當(dāng)前實(shí)例的直接子組件。需要注意$children并不保證順序,也不是響應(yīng)式的。如果正在嘗試使用$children來(lái)進(jìn)行數(shù)據(jù)綁定,考慮使用一個(gè)數(shù)組配合v-for來(lái)生成子組件,并且使用Array作為真正的來(lái)源
<div id="example"> <parent-component></parent-component> </div> <template id="parent-component"> <div class="parent"> <h3>我是父組件</h3> <button @click="getData">獲取子組件數(shù)據(jù)</button> <br> <div v-html="msg"></div> <child-component1></child-component1> <child-component2></child-component2> </div> </template> <template id="child-component1"> <div class="child"> <h3>我是子組件1</h3> <input v-model="msg"> <p>{{msg}}</p> </div> </template> <template id="child-component2"> <div class="child"> <h3>我是子組件2</h3> <input v-model="msg"> <p>{{msg}}</p> </div> </template>
<script> // 注冊(cè) Vue.component('parent-component', { template: '#parent-component', data(){ return{ msg:'', } }, methods:{ getData(){ let html = ''; let children = this.$children; for(var i = 0; i < children.length;i++){ html+= '<div>' + children[i].msg + '</div>'; } this.msg = html; } }, components:{ 'child-component1':{ template:'#child-component1', data(){ return{ msg:'', } }, }, 'child-component2':{ template:'#child-component2', data(){ return{ msg:'', } }, }, } }) // 創(chuàng)建根實(shí)例 new Vue({ el: '#example', }) </script>
$refs
組件個(gè)數(shù)較多時(shí),難以記住各個(gè)組件的順序和位置,通過(guò)序號(hào)訪問(wèn)子組件不是很方便
在子組件上使用ref屬性,可以給子組件指定一個(gè)索引ID:
<child-component1 ref="c1"></child-component1> <child-component2 ref="c2"></child-component2>
在父組件中,則通過(guò)$refs.索引ID訪問(wèn)子組件的實(shí)例
this.$refs.c1
this.$refs.c2
<div id="example"> <parent-component></parent-component> </div> <template id="parent-component"> <div class="parent"> <h3>我是父組件</h3> <div> <button @click="getData1">獲取子組件c1的數(shù)據(jù)</button> <p>{{msg1}}</p> </div> <div> <button @click="getData2">獲取子組件c2的數(shù)據(jù)</button> <p>{{msg2}}</p> </div> <child-component1 ref="c1"></child-component1> <child-component2 ref="c2"></child-component2> </div> </template> <template id="child-component1"> <div class="child"> <h3>我是子組件1</h3> <input v-model="msg"> <p>{{msg}}</p> </div> </template> <template id="child-component2"> <div class="child"> <h3>我是子組件2</h3> <input v-model="msg"> <p>{{msg}}</p> </div> </template>
<script> // 注冊(cè) Vue.component('parent-component', { template: '#parent-component', data(){ return{ msg1:'', msg2:'', } }, methods:{ getData1(){ this.msg1 = this.$refs.c1.msg; }, getData2(){ this.msg2 = this.$refs.c2.msg; }, }, components:{ 'child-component1':{ template:'#child-component1', data(){ return{ msg:'', } }, }, 'child-component2':{ template:'#child-component2', data(){ return{ msg:'', } }, }, } }) // 創(chuàng)建根實(shí)例 new Vue({ el: '#example', }) </script>
總結(jié)
雖然vue提供了以上方式對(duì)組件實(shí)例進(jìn)行直接訪問(wèn),但并不推薦這么做。這會(huì)導(dǎo)致組件間緊密耦合,且自身狀態(tài)難以理解,所以盡量使用props、自定義事件以及內(nèi)容分發(fā)slot來(lái)傳遞數(shù)據(jù)。
相關(guān)文章
vue+elementUI實(shí)現(xiàn)右擊指定表格列的單元格顯示選擇框功能
這篇文章主要介紹了vue+elementUI實(shí)現(xiàn)右擊指定表格列的單元格顯示選擇框功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-03-03element-ui 關(guān)于獲取select 的label值方法
今天小編就為大家分享一篇element-ui 關(guān)于獲取select 的label值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Vue3中導(dǎo)入和使用組件幾種常見(jiàn)方法(.vue文件)
組件是Vue.js最強(qiáng)大的功能之一, 組件可以擴(kuò)展HTML元素,封裝可重用的代碼,下面這篇文章主要介紹了Vue3中導(dǎo)入和使用組件幾種常見(jiàn)方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09vue3?el-table結(jié)合seamless-scroll實(shí)現(xiàn)表格數(shù)據(jù)滾動(dòng)的思路詳解
這篇文章主要介紹了vue3?el-table結(jié)合seamless-scroll實(shí)現(xiàn)表格數(shù)據(jù)滾動(dòng),創(chuàng)建兩個(gè)table,隱藏第一個(gè)table的body部分,這樣就能得到一個(gè)固定的head,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07Vue項(xiàng)目部署在Spring Boot出現(xiàn)頁(yè)面空白問(wèn)題的解決方案
這篇文章主要介紹了Vue項(xiàng)目部署在Spring Boot出現(xiàn)頁(yè)面空白問(wèn)題的解決方案,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11