vue.js中$emit的理解

來源:互聯網
上載者:User

標籤:自訂事件   his   min   on()   com   ret   image   event   src   

官網介紹比較簡單

例子:$emit(‘increment1‘,[12,‘kkk‘]),直接看是懵逼的有沒有,可以先告訴你,就是觸發自訂事件increment1(或者函數名吧),[]為參數

上案例

<!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title></title>    </head>    <body>        <div id="counter-event-example">            <p>{{ total }}</p>            <button-counter v-on:increment1="incrementTotal"></button-counter>            <button-counter v-on:increment2="incrementTotal"></button-counter>        </div>    </body>    <script src="vue/vue.min.js"></script>    <script>        Vue.component(‘button-counter‘,{            template:‘<button v-on:click="increment">{{ counter }}</button>‘,            data:function(){                return {counter: 0}//組件資料就是這樣的,函數式的,請注意            },            methods:{                increment:function(){                    this.counter += 1;                    this.$emit(‘increment1‘,[12,‘kkk‘]);//$emit                }            }        });        new Vue({            el:‘#counter-event-example‘,            data:{                total:0            },            methods:{                incrementTotal:function(e){                    this.total += 1;                    console.log(e);                }            }        });    </script></html>

先看組件 button-counter

綁定了事件click————>increment

然後 this.counter += 1; this.$emit(‘increment1‘,[12,‘kkk‘]);

這邊就是觸發事件 increment1,參考文獻裡面有點亂,這邊是不是清晰多了

然後 <button-counter v-on:increment1="incrementTotal"></button-counter> 

v-on相當於監聽吧,就觸發 incrementTotal

輸出// [12, "kkk"]

有沒有很清晰,若有理解不對的地方,請指正


 

 

 

 

 

參考:http://arinu.me/?p=50

vue.js中$emit的理解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.