Vue組件實例間直接訪問的示例分析-創(chuàng)新互聯(lián)

小編給大家分享一下Vue組件實例間直接訪問的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)建站于2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目成都做網(wǎng)站、網(wǎng)站設計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元江南做網(wǎng)站,已為上家服務,為江南各地企業(yè)和個人服務,聯(lián)系電話:13518219792

前面的話

  有時候需要父組件訪問子組件,子組件訪問父組件,或者是子組件訪問根組件。 在組件實例中,Vue提供了相應的屬性,包括$parent、$children、$refs和$root,這些屬性都掛載在組件的this上。

$parent 

  $parent表示父組件的實例,該屬性只讀

  下面是一個簡易實例

<div id="example">
 <parent-component></parent-component>
</div>
<template id="parent-component">
 <div class="parent">
 <h4>我是父組件</h4>
 <input v-model="parentMsg">
 <p>{{parentMsg}}</p>
 <child-component></child-component> 
 </div>
</template>
<template id="child-component">
 <div class="child">
 <h4>我是子組件</h4>
 <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表示當前組件樹的根 Vue 實例。如果當前實例沒有父實例,此實例將會是其自己。該屬性只讀

<div id="example">
 <h4>我是根組件</h4>
 <input v-model="rootMsg">
 <p>{{rootMsg}}</p> 
 <parent-component></parent-component>
</div>
<template id="parent-component">
 <div class="parent">
 <h4>我是父組件</h4>
 <input v-model="parentMsg">
 <p>{{parentMsg}}</p>
 <child-component></child-component> 
 </div>
</template>
<template id="child-component">
 <div class="child">
 <h4>我是子組件</h4>
 <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表示當前實例的直接子組件。需要注意$children并不保證順序,也不是響應式的。如果正在嘗試使用$children來進行數(shù)據(jù)綁定,考慮使用一個數(shù)組配合v-for來生成子組件,并且使用Array作為真正的來源

<div id="example">
 <parent-component></parent-component>
</div>
<template id="parent-component">
 <div class="parent">
 <h4>我是父組件</h4>
 <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">
 <h4>我是子組件1</h4>
 <input v-model="msg">
 <p>{{msg}}</p>
 </div>
</template>
<template id="child-component2">
 <div class="child">
 <h4>我是子組件2</h4>
 <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">
 <h4>我是父組件</h4>
 <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">
 <h4>我是子組件1</h4>
 <input v-model="msg">
 <p>{{msg}}</p>
 </div>
</template>
<template id="child-component2">
 <div class="child">
 <h4>我是子組件2</h4>
 <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>

以上是“Vue組件實例間直接訪問的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)成都做網(wǎng)站行業(yè)資訊頻道!

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

新聞標題:Vue組件實例間直接訪問的示例分析-創(chuàng)新互聯(lián)
鏈接地址:http://bm7419.com/article36/hcipg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供ChatGPT、微信公眾號、響應式網(wǎng)站、自適應網(wǎng)站、搜索引擎優(yōu)化、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設公司