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

vue中子組件向父組件傳遞數(shù)據(jù)的實(shí)例代碼(實(shí)現(xiàn)加減功能)

 更新時(shí)間:2018年04月20日 09:34:25   投稿:mrr  
這篇文章主要介紹了vue中子組件向父組件傳遞數(shù)據(jù)的實(shí)例代碼(實(shí)現(xiàn)加減功能) ,需要的朋友可以參考下

這里講解一下子組件向父組件傳遞值的常用方式。 這里通過(guò)一個(gè)加減法的實(shí)例向大家說(shuō)明一下,這個(gè)的原理。

如下圖所示:

當(dāng)沒(méi)有任何操作的時(shí)候父組件的值是 0

這里寫(xiě)圖片描述

當(dāng)點(diǎn)擊加號(hào)以后父組件的值是 1

這里寫(xiě)圖片描述

當(dāng)點(diǎn)擊減號(hào)以后父組件的值是減一變成 0

這里寫(xiě)圖片描述

具體代碼我直接貼出來(lái),剛出爐的代碼。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>子組件將數(shù)據(jù)傳遞給父組件</title>
  <script src="https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js"></script>
</head>
<script>
//定義一個(gè)組件
Vue.component('counter', {
 template: '\
    <div style="background:#eee;width: 238px;">\
        <div>這里是子組件里面的內(nèi)容!</div>\
        <div style="margin-top:20px"></div>\
        <div>\
          <span style="margin-right:20px;display:inline-block;">加法運(yùn)算</span><button @click="incrementCounter">+</button>\
        </div>\
        <div>\
          <span style="margin-right:20px;margin-top:20px;display:inline-block;">減法運(yùn)算</span><button @click="deleteCounter">-</button>\
        </div>\
    </div>\
  ',
 data: function () {
  return {
   counter: 0
  }
 },
 methods: {
  incrementCounter: function () {
   this.counter += 1;
   this.$emit('increment',1);
  },
  deleteCounter: function () {
   this.counter -= 1;
   this.$emit('increment',2);
  }
 }
})
//執(zhí)行一個(gè)組件
window.onload = function(){
  var app = new Vue({
    el: '#app',
    data: {
      total: 0
    },
    methods:{
      incrementTotal: function (val) {
        if(val==1){
          this.total += 1;
        }else{
          if(this.total<=0){
            this.total = 0;
          }else{
            this.total -= 1;
          }
        }
      }
    }
  })
}
</script>
<body>
  <div id="app" style="background:red;width: 238px;">
    <p>這里是父組件里面的內(nèi)容!</p>    
    <p>子組件傳遞的值:<b>{{ total }}</b></p>
    <counter v-on:increment="incrementTotal"></counter>
  </div>
</body>
</html>

總結(jié)

以上所述是小編給大家介紹的vue中子組件向父組件傳遞數(shù)據(jù)的實(shí)例代碼(實(shí)現(xiàn)加減功能) ,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

滦平县| 宁阳县| 从化市| 浦东新区| 德昌县| 榆社县| 平武县| 天镇县| 北流市| 晋城| 铁岭县| 临湘市| 石阡县| 普兰县| 山丹县| 宽城| 齐齐哈尔市| 大埔区| 长阳| 美姑县| 广水市| 云霄县| 蛟河市| 永清县| 错那县| 信阳市| 句容市| 区。| 略阳县| 雅安市| 敦化市| 漾濞| 开平市| 永康市| 张掖市| 从化市| 视频| 黔西县| 沿河| 枣阳市| 方山县|