有時候須要父組件訪問子組件,子組件訪問父組件,或者是子組件訪問根組件。 在組件實例中,Vue提供了相應的屬性,包括$parent、$children、$refs和$root,這些屬性都掛載在組件的this上。本文將詳細介紹Vue組件實例間的直接訪問html
$parent表示父組件的實例,該屬性只讀vue
下面是一個簡易實例數組
<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">顯示父組件數據</button> </div> </template>
<script> // 註冊 Vue.component('parent-component', { template: '#parent-component', data(){ return{ parentMsg:'我是父組件的數據' } }, components:{ 'child-component':{ template:'#child-component', data(){ return{ msg:'' } }, methods:{ showData(){ this.msg = this.$parent.parentMsg; } } } } }) // 建立根實例 new Vue({ el: '#example' }) </script>
$root表示當前組件樹的根 Vue 實例。若是當前實例沒有父實例,此實例將會是其本身。該屬性只讀this
<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">顯示根組件數據</button><span>{{rootMsg}}</span> </p> <p> <button v-on:click="showParentData">顯示父組件數據</button><span>{{parentMsg}}</span> </p> </div> </template>
<script> // 註冊 Vue.component('parent-component', { template: '#parent-component', data(){ return{ parentMsg:'我是父組件的數據' } }, components:{ 'child-component':{ template:'#child-component', data(){ return{ parentMsg:'', rootMsg:'' } }, methods:{ showParentData(){ this.parentMsg = this.$parent.parentMsg; }, showRootData(){ this.rootMsg = this.$root.rootMsg; }, } } } }) // 建立根實例 new Vue({ el: '#example', data:{ rootMsg:'我是根組件數據' } }) </script>
$children表示當前實例的直接子組件。須要注意$children
並不保證順序,也不是響應式的。若是正在嘗試使用$children
來進行數據綁定,考慮使用一個數組配合v-for
來生成子組件,而且使用Array做爲真正的來源spa
<div id="example"> <parent-component></parent-component> </div> <template id="parent-component"> <div class="parent"> <h3>我是父組件</h3> <button @click="getData">獲取子組件數據</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:'', } }, }, } }) // 建立根實例 new Vue({ el: '#example', }) </script>
組件個數較多時,難以記住各個組件的順序和位置,經過序號訪問子組件不是很方便code
在子組件上使用ref屬性,能夠給子組件指定一個索引ID:component
<child-component1 ref="c1"></child-component1> <child-component2 ref="c2"></child-component2>
在父組件中,則經過$refs.索引ID
訪問子組件的實例htm
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的數據</button> <p>{{msg1}}</p> </div> <div> <button @click="getData2">獲取子組件c2的數據</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:'', } }, }, } }) // 建立根實例 new Vue({ el: '#example', }) </script>
雖然vue提供了以上方式對組件實例進行直接訪問,但並不推薦這麼作。這會致使組件間緊密耦合,且自身狀態難以理解,因此儘可能使用props、自定義事件以及內容分發slot來傳遞數據blog