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