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

Vue.js組件通信之自定義事件詳解

 更新時(shí)間:2019年10月19日 09:14:10   作者:吳聲子夜歌  
這篇文章主要為大家詳細(xì)介紹了Vue.js組件通信之自定義事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

組件通信

從父組件向子組件通信,通過(guò)props傳遞數(shù)據(jù)就可以了,但Vue組件通信的場(chǎng)景不止有這一種,歸納起來(lái),組件之間的通信可以用下圖來(lái)表示:

自定義事件

當(dāng)子組件需要向父組件傳遞數(shù)據(jù)時(shí),就要用到自定義事件。子組件用**$ emit()來(lái)觸發(fā)事件**,父組件用**$ on()**來(lái)監(jiān)聽(tīng)子組件的事件。

父組件也可以直接在子組件的自定義標(biāo)簽上使用v-on來(lái)監(jiān)聽(tīng)子組件觸發(fā)的事件。

<!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">
  <script src="https://unpkg.com/vue/dist/vue.js"></script>
  <title>自定義事件</title>
</head>
<body>
  <div id="app">
    <p>總數(shù):{{total}}</p>
    <my-component
      @increase="handleGetTotal"
      @reduce="handleGetTotal"></my-component>
  </div>
  <script>
    Vue.component('my-component',{
      template: '\
        <div>\
          <button @click="handleIncrease">+1</button>\
          <button @click="handleReduce">-1</button>\
        </div>',
      data: function () {
        return {
          counter: 0
        }
      },
      methods: {
        handleIncrease: function () {
          this.counter++;
          this.$emit('increase', this.counter);
        },
        handleReduce: function () {
          this.counter--;
          this.$emit('reduce', this.counter);
        }
      }
    });

    var app = new Vue({
      el: '#app',
      data: {
        total: 0
      },
      methods: {
        handleGetTotal: function (total) {
          this.total = total;
        }
      }
    });
  </script>
</body>
</html>

子組件有兩個(gè)按鈕,分別實(shí)現(xiàn)+1和-1的效果,在改變組件的data “counter”后,通過(guò)$emit()在把它傳遞給父組件,父組件使用v-on:increase和v-on:reduce監(jiān)聽(tīng)事件。

$emit()方法的第一個(gè)參數(shù)是自定義事件的名稱(chēng),后面的參數(shù)是要傳遞的數(shù)據(jù),可以不填或者填寫(xiě)多個(gè)。

注意:除了用v-on在組件上監(jiān)聽(tīng)自定義事件外,也可以監(jiān)聽(tīng)DOM事件,這時(shí)候可以用 .native修飾符表示監(jiān)聽(tīng)的是一個(gè)原生事件,監(jiān)聽(tīng)的是該組件的根元素:

<my-component v-on:click.native="handleClick"></my-component>

使用v-model

Vue 2.x 可以在自定義組件上使用v-model指令。

<!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">
  <script src="https://unpkg.com/vue/dist/vue.js"></script>
  <title>使用v-model</title>
</head>
<body>
  <div id="app">
    <p>總數(shù):{{total}}</p>
    <my-component v-model="total"></my-component>
  </div>
  <script>
    Vue.component('my-component',{
      template: '<button @click="handleClick">+1</button>',
      data: function () {
        return {
          counter: 0
        }
      },
      methods: {
        handleClick: function () {
          this.counter++;
          this.$emit('input',this.counter);
        }
      }
    });

    var app = new Vue({
      el: '#app',
      data: {
        total: 0
      }
    });
  </script>
</body>
</html>

仍然是點(diǎn)擊按鈕+1的效果,不過(guò)這次組件$emit()的事件是特殊的input,在使用組件的父級(jí),并沒(méi)有在<my-component>上使用@input=“handler”,而是使用了v-model板頂?shù)囊粋€(gè)數(shù)據(jù)total。這也可以稱(chēng)作是一個(gè)語(yǔ)法糖,因?yàn)樯厦娴氖纠梢蚤g接地用自定義事件來(lái)實(shí)現(xiàn):

<div id="myApp">
  <p>總數(shù):{{total}}</p>
  <my-component1 @input="handlegetTotal"></my-component1>
</div>
<script>
  Vue.component('my-component1',{
    template: '<button @click="handleClick">+1</button>',
    data: function () {
      return {
        counter: 0
      }
    },
    methods: {
      handleClick: function () {
        this.counter++;
        this.$emit('input',this.counter);
      }
    }
  });

  var myApp = new Vue({
    el: '#myApp',
    data: {
      total: 0
    },
    methods: {
      handlegetTotal: function (value) {
        this.total = value;
      }
    }
  });
</script>

v-model還可以用來(lái)創(chuàng)建自定義的表單輸入組件,進(jìn)行數(shù)據(jù)雙向綁定:

<!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">
  <script src="https://unpkg.com/vue/dist/vue.js"></script>
  <title>自定義表單輸入組件</title>
</head>
<body>
  <div id="app">
    <p>總數(shù):{{total}}</p>
    <my-component v-model="total"></my-component>
    <button @click="handleReduce">-1</button>
  </div>
  <script>
    Vue.component('my-component',{
      props: ['value'],
      template: '<input :value="value" @input="updateValue">',
      methods: {
        updateValue: function (event) {
          this.$emit('input', event.target.value);
        }
      }
    });

    var app = new Vue({
      el: '#app',
      data: {
        total: 0
      },
      methods: {
        handleReduce: function () {
          this.total--;
        }
      }
    });
  </script>
</body>
</html>

注意:實(shí)現(xiàn)這樣一個(gè)具有雙向綁定的v-model組件要滿足下面的兩個(gè)要求:

  • 接受一個(gè)value屬性
  • 在有新的value時(shí)觸發(fā)input事件

更多教程點(diǎn)擊《Vue.js前端組件學(xué)習(xí)教程》,歡迎大家學(xué)習(xí)閱讀。

關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專(zhuān)題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。

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

相關(guān)文章

最新評(píng)論

井研县| 惠水县| 安图县| 东乡县| 嵩明县| 大同县| 巴彦县| 闽清县| 横山县| 六安市| 宁津县| 汕头市| 托里县| 临夏县| 天气| 会东县| 惠来县| 九寨沟县| 黎平县| 临武县| 牙克石市| 徐水县| 巧家县| 阳谷县| 新竹市| 玉溪市| 衢州市| 车致| 宝鸡市| 怀柔区| 巢湖市| 北碚区| 清流县| 双牌县| 龙里县| 会泽县| 宜君县| 玛曲县| 靖宇县| 阿荣旗| 宁安市|