最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue組件實例間的直接訪問實現(xiàn)代碼

 更新時間:2017年08月20日 09:38:11   作者:小火柴的藍色理想  
在組件實例中,Vue提供了相應(yīng)的屬性,包括$parent、$children、$refs和$root,這些屬性都掛載在組件的this上。本文將詳細介紹Vue組件實例間的直接訪問,需要的朋友可以參考下

前面的話

  有時候需要父組件訪問子組件,子組件訪問父組件,或者是子組件訪問根組件。 在組件實例中,Vue提供了相應(yīng)的屬性,包括$parent、$children、$refs和$root,這些屬性都掛載在組件的this上。本文將詳細介紹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來進行數(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提供了以上方式對組件實例進行直接訪問,但并不推薦這么做。這會導(dǎo)致組件間緊密耦合,且自身狀態(tài)難以理解,所以盡量使用props、自定義事件以及內(nèi)容分發(fā)slot來傳遞數(shù)據(jù)。

相關(guān)文章

  • Vue函數(shù)式組件專篇深入分析

    Vue函數(shù)式組件專篇深入分析

    Vue提供了一種稱為函數(shù)式組件的組件類型,用來定義那些沒有響應(yīng)數(shù)據(jù),也不需要有任何生命周期的場景,它只接受一些props來顯示組件,下面這篇文章主要給大家介紹了關(guān)于Vue高級組件之函數(shù)式組件的使用場景與源碼分析的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • 使用Vuex實現(xiàn)一個筆記應(yīng)用的方法

    使用Vuex實現(xiàn)一個筆記應(yīng)用的方法

    這篇文章主要介紹了使用Vuex實現(xiàn)一個筆記應(yīng)用的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • 淺談使用mpvue開發(fā)小程序需要注意和了解的知識點

    淺談使用mpvue開發(fā)小程序需要注意和了解的知識點

    這篇文章主要介紹了淺談使用mpvue開發(fā)小程序需要注意和了解的知識點,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • vue element和nuxt的使用技巧分享

    vue element和nuxt的使用技巧分享

    這篇文章主要介紹了vue element和nuxt的使用技巧分享,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下
    2021-01-01
  • 詳解vuejs之v-for列表渲染

    詳解vuejs之v-for列表渲染

    這篇文章主要介紹了詳解vuejs之v-for列表渲染,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Vue首頁加載過慢的解決方式

    Vue首頁加載過慢的解決方式

    這篇文章主要介紹了Vue首頁加載過慢的解決方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • vue中computed和watch的使用實例代碼解析

    vue中computed和watch的使用實例代碼解析

    這篇文章主要介紹了vue中computed和watch的綜合運用實例,主要需求是點擊按鈕實現(xiàn)天氣的切換效果結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-04-04
  • vue中本地靜態(tài)圖片的路徑應(yīng)該怎么寫

    vue中本地靜態(tài)圖片的路徑應(yīng)該怎么寫

    這篇文章主要介紹了vue中本地靜態(tài)圖片的路徑應(yīng)該怎么寫,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 解決vue-cli3 使用子目錄部署問題

    解決vue-cli3 使用子目錄部署問題

    這篇文章主要介紹了解決vue-cli3 使用子目錄部署問題,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • Vue通過echarts實現(xiàn)數(shù)據(jù)圖表化顯示

    Vue通過echarts實現(xiàn)數(shù)據(jù)圖表化顯示

    Echarts,它是一個與框架無關(guān)的 JS 圖表庫,但是它基于Js,這樣很多框架都能使用它,例如Vue,估計IONIC也能用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08

最新評論

武汉市| 南华县| 论坛| 莒南县| 化隆| 南郑县| 渑池县| 淮安市| 和龙市| 威宁| 漳州市| 措勤县| 疏勒县| 乌拉特中旗| 江源县| 肇东市| 新巴尔虎右旗| 隆回县| 清新县| 亚东县| 隆子县| 高州市| 商河县| 张家口市| 平顶山市| 福清市| 女性| 从化市| 天气| 寻乌县| 彭水| 许昌县| 濮阳市| 乐亭县| 长葛市| 鲜城| 平昌县| 新闻| 宁化县| 郸城县| 合水县|