欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年08月20日 09:38:11   作者:小火柴的藍(lán)色理想  
在組件實(shí)例中,Vue提供了相應(yīng)的屬性,包括$parent、$children、$refs和$root,這些屬性都掛載在組件的this上。本文將詳細(xì)介紹Vue組件實(shí)例間的直接訪問(wè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 中 命名視圖的用法實(shí)例詳解

    vue 中 命名視圖的用法實(shí)例詳解

    這篇文章主要介紹了vue 中 命名視圖的用法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2019-08-08
  • vue+elementUI實(shí)現(xiàn)右擊指定表格列的單元格顯示選擇框功能

    vue+elementUI實(shí)現(xiàn)右擊指定表格列的單元格顯示選擇框功能

    這篇文章主要介紹了vue+elementUI實(shí)現(xiàn)右擊指定表格列的單元格顯示選擇框功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-03-03
  • vue實(shí)現(xiàn)右鍵彈出菜單

    vue實(shí)現(xiàn)右鍵彈出菜單

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)右鍵彈出菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • element-ui 關(guān)于獲取select 的label值方法

    element-ui 關(guān)于獲取select 的label值方法

    今天小編就為大家分享一篇element-ui 關(guān)于獲取select 的label值方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • 在Vue中使用Select選擇器拼接label的操作

    在Vue中使用Select選擇器拼接label的操作

    這篇文章主要介紹了在Vue中使用Select選擇器拼接label的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • vuex的輔助函數(shù)該如何使用

    vuex的輔助函數(shù)該如何使用

    vue通過(guò)輔助函數(shù)mapState、mapActions、mapMutations,把vuex.store中的屬性映射到vue實(shí)例身上,這樣在vue實(shí)例中就能訪問(wèn)vuex.store中的屬性了,對(duì)于操作vuex.store就很方便了,本文具體的介紹下這些輔助函數(shù)的使用方法
    2021-06-06
  • Vue3中導(dǎo)入和使用組件幾種常見(jiàn)方法(.vue文件)

    Vue3中導(dǎo)入和使用組件幾種常見(jiàn)方法(.vue文件)

    組件是Vue.js最強(qiáng)大的功能之一, 組件可以擴(kuò)展HTML元素,封裝可重用的代碼,下面這篇文章主要介紹了Vue3中導(dǎo)入和使用組件幾種常見(jiàn)方法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-09-09
  • vue3?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)的思路詳解

    這篇文章主要介紹了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-07
  • Vue項(xiàng)目部署在Spring Boot出現(xiàn)頁(yè)面空白問(wèn)題的解決方案

    Vue項(xiàng)目部署在Spring Boot出現(xiàn)頁(yè)面空白問(wèn)題的解決方案

    這篇文章主要介紹了Vue項(xiàng)目部署在Spring Boot出現(xiàn)頁(yè)面空白問(wèn)題的解決方案,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11
  • 如何在Vue中使用protobuf

    如何在Vue中使用protobuf

    這篇文章主要介紹了如何在Vue中使用protobuf,protobuf是由google推出的和語(yǔ)言無(wú)關(guān)和平臺(tái)無(wú)關(guān),幾乎支持當(dāng)前的大部分語(yǔ)言,如JavaScript,下文更多相關(guān)介紹需要的小伙伴可以參考一下
    2022-03-03

最新評(píng)論