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

vue子組件使用自定義事件向父組件傳遞數(shù)據(jù)

 更新時間:2017年05月27日 15:54:57   作者:fantasy-zxf  
這篇文章主要介紹了vue子組件使用自定義事件向父組件傳遞數(shù)據(jù)的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

使用v-on綁定自定義事件可以讓子組件向父組件傳遞數(shù)據(jù),用到了this.$emit(‘自定義的事件名稱',傳遞給父組件的數(shù)據(jù))

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <script src="../js/vue.js"></script>
</head>
<body>
<div id="app">
<parent-component></parent-component>
</div>
<template id="parent-component">
<div>
 <p>總數(shù)是{{total}}</p>
 <child-component @increment="incrementTotal"></child-component>
 <!--@increment是子組件this.$emit('increment'自定義的事件用來告訴父組件自己干了什么事
 然后會觸發(fā)父子間incrementTotal的方法來更新父組件的內容)-->
</div>
</template>
<template id="child-component">
 <button @click="increment()">{{mycounter}}</button>
</template>
<script>
 var child=Vue.extend({
  template:"#child-component",
  data:function () {
   return {
    mycounter:0
   }
  },
  methods:{
   increment:function(){
    this.mycounter=10;
    this.$emit("increment",this.mycounter);//把this.mycounter傳遞給父組件
   }
  }
 })
 var parent=Vue.extend({
  data:function () {
   return {
    total:0
   }
  },
  methods:{
   incrementTotal:function(newValue){
    this.total+=newValue;
   }
  },
  template:"#parent-component",
  components:{
   'child-component':child
  }
 })
 var vm=new Vue({
  el:"#app",
  components:{
   'parent-component':parent
  }
 })
</script>
</body>
</html>

@increment是子組件this.$emit('increment'自定義的事件,newValue)用來告訴父組件自己干了什么事

同時還可以傳遞新的數(shù)據(jù)給父組件

然后會觸發(fā)父子間incrementTotal的方法來更新父組件的內容)。

這里需要注意幾個點:

1.

圖中紅色圈中的部分是對應的,子組件在自己的methods方法里面寫自己的事件實現(xiàn),然后再父組件里面寫字組件時給子組件綁定上methods方法里面的

事件名稱,要一一對應

這里自定義事件里面的要寫父組件的方法名,父組件里面定義該方法。

父組件里面的方法可以接收子組件this.$emit('increment',this.mycounter);傳遞過來的數(shù)值:this.mycounter,

到父組件的方法里面就是newValue參數(shù),這樣就實現(xiàn)了子組件向父組件傳遞數(shù)據(jù)

以上所述是小編給大家介紹的vue子組件使用自定義事件向父組件傳遞數(shù)據(jù),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關文章

最新評論

楚雄市| 宝兴县| 南康市| 惠州市| 会理县| 龙游县| 塔城市| 临西县| 宁化县| 四川省| 凤阳县| 林周县| 广德县| 鲁山县| 皋兰县| 安丘市| 定襄县| 晋中市| 即墨市| 洪江市| 开封市| 白银市| 文登市| 洛南县| 承德市| 盐城市| 和平区| 盘锦市| 维西| 清徐县| 阳新县| 于都县| 高尔夫| 禄丰县| 北票市| 茶陵县| 阿合奇县| 鄂托克旗| 宕昌县| 禹城市| 隆子县|