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

Vue2單一事件管理組件通信

 更新時(shí)間:2017年05月09日 15:05:59   作者:scorpio_h  
這篇文章主要為大家詳細(xì)介紹了Vue2單一事件管理組件通信的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家分享了vue $emit 和 $on 組件通信,供大家參考,具體內(nèi)容如下

<!DOCTYPE html> 
<html lang="en"> 
<head> 
 <meta charset="UTF-8" /> 
 <title>Vue2-單一事件管理組件通信</title> 
 <script src="vue.js"></script> 
 <script type="text/javascript"> 
 
 //準(zhǔn)備一個(gè)空的實(shí)例對(duì)象 
 var Event = new Vue(); 
 
 //組件A 
 var A = { 
  template: ` 
   <div> 
    <span>我是A組件的數(shù)據(jù)->{{a}}</span> 
    <input type="button" value="把A數(shù)據(jù)傳給C" @click = "send"> 
   </div> 
  `, 
  methods: { 
   send () { 
    Event.$emit("a-msg", this.a); 
   } 
  }, 
  data () { 
   return { 
    a: "我是a組件中數(shù)據(jù)" 
   } 
  } 
 }; 
 //組件B 
 var B = { 
  template: ` 
   <div> 
    <span>我是B組件的數(shù)據(jù)->{{a}}</span> 
    <input type="button" value="把B數(shù)據(jù)傳給C" @click = "send"> 
   </div> 
  `, 
  methods: { 
   send () { 
    Event.$emit("b-msg", this.a); 
   } 
  }, 
  data () { 
   return { 
    a: "我是b組件中數(shù)據(jù)" 
   } 
  } 
 }; 
 //組件C 
 var C = { 
  template: ` 
   <div> 
    <h3>我是C組件</h3> 
    <span>接收過來A的數(shù)據(jù)為: {{a}}</span> 
    <br> 
    <span>接收過來B的數(shù)據(jù)為: {}</span> 
   </div> 
  `, 
  mounted () { 
   //接收A組件的數(shù)據(jù) 
   Event.$on("a-msg", function (a) { 
    this.a = a; 
   }.bind(this)); 
 
   //接收B組件的數(shù)據(jù) 
   Event.$on("b-msg", function (a) { 
    this.b = a; 
   }.bind(this)); 
  }, 
  data () { 
   return { 
    a: "", 
    b: "" 
   } 
  } 
 }; 
 window.onload = function () { 
  new Vue({ 
   el: "#box", 
   components: { 
    "dom-a": A, 
    "dom-b": B, 
    "dom-c": C 
   } 
  }); 
 }; 
 
 
 </script> 
</head> 
<body> 
 <div id="box"> 
  <dom-a></dom-a>  
  <dom-b></dom-b>  
  <dom-c></dom-c>  
 </div> 
 
</body> 
</html> 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論