av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

8種vue組件通信方式詳細(xì)解析實(shí)例

對(duì)于vue來(lái)說(shuō),組件是非常常見(jiàn)的,有很多平臺(tái)都封裝了了屬于自己一套的組件,如element ui、we ui等等。同時(shí)組件之間的消息傳遞也是非常重要的,下面是我對(duì)組件之間消息傳遞的各種方式的總結(jié),共有8種方式。如有不足之處,可以留言補(bǔ)充,互相學(xué)習(xí)。

我們提供的服務(wù)有:網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、壽光ssl等。為上千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢(xún)和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的壽光網(wǎng)站制作公司

1. props和$emit

這是最最常用的父子組件通信方式,父組件向子組件傳遞數(shù)據(jù)是通過(guò)prop傳遞的,子組件傳遞數(shù)據(jù)給父組件是通過(guò)$emit觸發(fā)事件來(lái)做到的。 實(shí)例:

父組件

 
 
 
 
  1. Vue.component('parent',{ 
  2.  template:` 
  3.  
     
  4.  

    父組件

     
  5.   
  6.  
 
  •  `, 
  •  data(){ 
  •  return { 
  •  message:'Hello web秀' 
  •  } 
  •  }, 
  •  methods:{ 
  •  //執(zhí)行子組件觸發(fā)的事件 
  •  getChildData(val){ 
  •  console.log(val) 
  •  } 
  •  } 
  • }) 
  • var app=new Vue({ 
  •  el:'#app', 
  •  template:` 
  •  
     
  •   
  •  
  •  
  •  ` 
  • }) 
  • 子組件

     
     
     
     
    1. Vue.component('child',{ 
    2.  //得到父組件傳遞過(guò)來(lái)的數(shù)據(jù) 
    3.  props:['message'], 
    4.  data(){ 
    5.  return { 
    6.  myMessage: this.message 
    7.  } 
    8.  }, 
    9.  template:` 
    10.  
       
    11.    
    12.  
     
  •  `, 
  •  methods:{ 
  •  passData(val){ 
  •  //觸發(fā)父組件中的事件 
  •  this.$emit('getChildData',val) 
  •  } 
  •  } 
  • }) 
  • 解析:

    2. $attrs和$listeners

    ***種方式處理父子組件之間的數(shù)據(jù)傳輸有一個(gè)問(wèn)題:如果多層嵌套,父組件A下面有子組件B,組件B下面有組件C,這時(shí)如果組件A想傳遞數(shù)據(jù)給組件C怎么辦呢?

    如果采用***種方法,我們必須讓組件A通過(guò)prop傳遞消息給組件B,組件B在通過(guò)prop傳遞消息給組件C;要是組件A和組件C之間有更多的組件,那采用這種方式就很復(fù)雜了。從Vue 2.4開(kāi)始,提供了$attrs和$listeners來(lái)解決這個(gè)問(wèn)題,能夠讓組件A之間傳遞消息給組件C。

    C組件

     
     
     
     
    1. Vue.component('C',{ 
    2.  template:` 
    3.  
       
    4.   
    5.  
     
  •  `, 
  •  methods:{ 
  •  passCData(val){ 
  •  //觸發(fā)父組件A中的事件 
  •  this.$emit('getCData',val) 
  •  } 
  •  } 
  • }) 
  • B組件

     
     
     
     
    1. Vue.component('B',{ 
    2.  data(){ 
    3.  return { 
    4.  myMessage:this.message 
    5.  } 
    6.  }, 
    7.  template:` 
    8.  
       
    9.   
    10.   
    11.  
     
  •  `, 
  •  //得到父組件傳遞過(guò)來(lái)的數(shù)據(jù) 
  •  props:['message'], 
  •  methods:{ 
  •  passData(val){ 
  •  //觸發(fā)父組件中的事件 
  •  this.$emit('getChildData',val) 
  •  } 
  •  } 
  • }) 
  • A組件

     
     
     
     
    1. Vue.component('A',{ 
    2.  template:` 
    3.  
       
    4.  

      this is parent compoent!

       
    5.  
    6.  :messageC="messageC"  
    7.  :message="message"  
    8.  v-on:getCData="getCData"  
    9.  v-on:getChildData="getChildData(message)"> 
    10.   
    11.   
    12.  `, 
    13.  data(){ 
    14.  return { 
    15.  message:'Hello', 
    16.  messageC:'Hello c' 
    17.  } 
    18.  }, 
    19.  methods:{ 
    20.  getChildData(val){ 
    21.  console.log('這是來(lái)自B組件的數(shù)據(jù)') 
    22.  }, 
    23.  //執(zhí)行C子組件觸發(fā)的事件 
    24.  getCData(val){ 
    25.  console.log("這是來(lái)自C組件的數(shù)據(jù):"+val) 
    26.  } 
    27.  } 
    28. }) 
    29. var app=new Vue({ 
    30.  el:'#app', 
    31.  template:` 
    32.  
       
    33.   
    34.   
    35.  ` 
    36. }) 

    解析:

    3. v-model

    父組件通過(guò)v-model傳遞值給子組件時(shí),會(huì)自動(dòng)傳遞一個(gè)value的prop屬性,在子組件中通過(guò)this.$emit(‘input',val)自動(dòng)修改v-model綁定的值

    子組件

     
     
     
     
    1. Vue.component('child',{ 
    2.  props:{ 
    3.  //v-model會(huì)自動(dòng)傳遞一個(gè)字段為value的prop屬性 
    4.  value: String,  
    5.  }, 
    6.  data(){ 
    7.  return { 
    8.  myMessage:this.value 
    9.  } 
    10.  }, 
    11.  methods:{ 
    12.  changeValue(){ 
    13.  //通過(guò)如此調(diào)用可以改變父組件上v-model綁定的值 
    14.  this.$emit('input',this.myMessage); 
    15.  } 
    16.  }, 
    17.  template:` 
    18.  
       
    19.  
    20.  type="text"  
    21.  v-model="myMessage"  
    22.  @change="changeValue"> 
    23.   
    24.  ` 
    25. }) 

    父組件

     
     
     
     
    1. Vue.component('parent',{ 
    2.  template:` 
    3.  
       
    4.  

      this is parent compoent!

       
    5.  

      {{message}}

       
    6.   
    7.   
    8.  `, 
    9.  data(){ 
    10.  return { 
    11.  message:'Hello' 
    12.  } 
    13.  } 
    14. }) 
    15. var app=new Vue({ 
    16.  el:'#app', 
    17.  template:` 
    18.  
       
    19.   
    20.   
    21.  ` 
    22. }) 

    4. provide和inject

    父組件中通過(guò)provider來(lái)提供變量,然后在子組件中通過(guò)inject來(lái)注入變量。不論子組件有多深,只要調(diào)用了inject那么就可以注入provider中的數(shù)據(jù)。而不是局限于只能從當(dāng)前父組件的prop屬性來(lái)獲取數(shù)據(jù),只要在父組件的生命周期內(nèi),子組件都可以調(diào)用。

    子組件

     
     
     
     
    1. Vue.component('child',{ 
    2.  inject:['for'],//得到父組件傳遞過(guò)來(lái)的數(shù)據(jù) 
    3.  data(){ 
    4.  return { 
    5.  myMessage: this.for 
    6.  } 
    7.  }, 
    8.  template:` 
    9.  
       
    10.   
    11.   
    12.  ` 
    13. }) 

    父組件

     
     
     
     
    1. Vue.component('parent',{ 
    2.  template:` 
    3.  
       
    4.  

      this is parent compoent!

       
    5.   
    6.   
    7.  `, 
    8.  provide:{ 
    9.  for:'test' 
    10.  }, 
    11.  data(){ 
    12.  return { 
    13.  message:'Hello' 
    14.  } 
    15.  } 
    16. }) 
    17. var app=new Vue({ 
    18.  el:'#app', 
    19.  template:` 
    20.  
       
    21.   
    22.   
    23.  ` 
    24. }) 

    5. 中央事件總線(xiàn)

    上面方式都是處理的父子組件之間的數(shù)據(jù)傳遞,那如果兩個(gè)組件不是父子關(guān)系呢?也就是兄弟組件如何通信?

    這種情況下可以使用中央事件總線(xiàn)的方式。新建一個(gè)Vue事件bus對(duì)象,然后通過(guò)bus.$emit觸發(fā)事件,bus.$on監(jiān)聽(tīng)觸發(fā)的事件。

     
     
     
     
    1. Vue.component('brother1',{ 
    2.  data(){ 
    3.  return { 
    4.  myMessage:'Hello brother1' 
    5.  } 
    6.  }, 
    7.  template:` 
    8.  
       
    9.  

      this is brother1 compoent!

       
    10.   
    11.   
    12.  `, 
    13.  methods:{ 
    14.  passData(val){ 
    15.  //觸發(fā)全局事件globalEvent 
    16.  bus.$emit('globalEvent',val) 
    17.  } 
    18.  } 
    19. }) 
    20. Vue.component('brother2',{ 
    21.  template:` 
    22.  
       
    23.  

      this is brother2 compoent!

       
    24.  

      brother1傳遞過(guò)來(lái)的數(shù)據(jù):{{brothermessage}}

       
    25.   
    26.  `, 
    27.  data(){ 
    28.  return { 
    29.  myMessage:'Hello brother2', 
    30.  brothermessage:'' 
    31.  } 
    32.  }, 
    33.  mounted(){ 
    34.  //綁定全局事件globalEvent 
    35.  bus.$on('globalEvent',(val)=>{ 
    36.  this.brothermessage=val; 
    37.  }) 
    38.  } 
    39. }) 
    40. //中央事件總線(xiàn) 
    41. var bus=new Vue(); 
    42. var app=new Vue({ 
    43.  el:'#app', 
    44.  template:` 
    45.  
       
    46.   
    47.   
    48.   
    49.  ` 
    50. }) 

    6. parent和children

     
     
     
     
    1. Vue.component('child',{ 
    2.     props:{ 
    3.       value:String, //v-model會(huì)自動(dòng)傳遞一個(gè)字段為value的prop屬性 
    4.     }, 
    5.     data(){ 
    6.       return { 
    7.         mymessage:this.value 
    8.       } 
    9.     }, 
    10.     methods:{ 
    11.       changeValue(){ 
    12.         this.$parent.message = this.mymessage;//通過(guò)如此調(diào)用可以改變父組件的值 
    13.       } 
    14.     }, 
    15.     template:` 
    16.       
       
    17.          
    18.        
    19.   }) 
    20.   Vue.component('parent',{ 
    21.     template:` 
    22.       
       
    23.         

      this is parent compoent!

       
    24.         test 
    25.          
    26.        
    27.     `, 
    28.     methods:{ 
    29.       changeChildValue(){ 
    30.         this.$children[0].mymessage = 'hello'; 
    31.       }//在此我向大家推薦一個(gè)前端全棧開(kāi)發(fā)交流圈:619586920 突破技術(shù)瓶頸,提升思維能力 
    32.     }, 
    33.     data(){ 
    34.       return { 
    35.         message:'hello' 
    36.       } 
    37.     } 
    38.   }) 
    39.   var app=new Vue({ 
    40.     el:'#app', 
    41.     template:` 
    42.       
       
    43.          
    44.        
    45.     ` 
    46.   }) 

    7. boradcast和dispatch

    vue1.0中提供了這種方式,但vue2.0中沒(méi)有,但很多開(kāi)源軟件都自己封裝了這種方式,比如min ui、element ui和iview等。 比如如下代碼,一般都作為一個(gè)mixins去使用, broadcast是向特定的父組件,觸發(fā)事件,dispatch是向特定的子組件觸發(fā)事件,本質(zhì)上這種方式還是on和on和emit的封裝,但在一些基礎(chǔ)組件中卻很實(shí)用。

     
     
     
     
    1. function broadcast(componentName, eventName, params) { 
    2.  this.$children.forEach(child => { 
    3.  var name = child.$options.componentName; 
    4.  if (name === componentName) { 
    5.  child.$emit.apply(child, [eventName].concat(params)); 
    6.  } else { 
    7.  broadcast.apply(child, [componentName, eventName].concat(params)); 
    8.  } 
    9.  }); 
    10. export default { 
    11.  methods: { 
    12.  dispatch(componentName, eventName, params) { 
    13.  var parent = this.$parent; 
    14.  var name = parent.$options.componentName; 
    15.  while (parent && (!name || name !== componentName)) { 
    16.  parentparent = parent.$parent; 
    17.  if (parent) { 
    18.  name = parent.$options.componentName; 
    19.  } 
    20.  } 
    21.  if (parent) { 
    22.  parent.$emit.apply(parent, [eventName].concat(params)); 
    23.  } 
    24.  }, 
    25.  broadcast(componentName, eventName, params) { 
    26.  broadcast.call(this, componentName, eventName, params); 
    27.  } 
    28.  } 
    29. }; 

    8. vuex處理組件之間的數(shù)據(jù)交互

    如果業(yè)務(wù)邏輯復(fù)雜,很多組件之間需要同時(shí)處理一些公共的數(shù)據(jù),這個(gè)時(shí)候才有上面這一些方法可能不利于項(xiàng)目的維護(hù),vuex的做法就是將這一些公共的數(shù)據(jù)抽離出來(lái),然后其他組件就可以對(duì)這個(gè)公共數(shù)據(jù)進(jìn)行讀寫(xiě)操作,這樣達(dá)到了解耦的目的。


    名稱(chēng)欄目:8種vue組件通信方式詳細(xì)解析實(shí)例
    分享地址:http://uogjgqi.cn/article/dpjipsh.html
    掃二維碼與項(xiàng)目經(jīng)理溝通

    我們?cè)谖⑿派?4小時(shí)期待你的聲音

    解答本文疑問(wèn)/技術(shù)咨詢(xún)/運(yùn)營(yíng)咨詢(xún)/技術(shù)建議/互聯(lián)網(wǎng)交流

    其他資訊