關于官網vue中v-on綁定自定義事件的個人理解
對官網實例進行了一些修改,如下圖:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>v-on綁定自定義事件</title> <script src="vue.js"></script> </head> <body> <div id="app"> <p>{{ total }}</p> <button-counter v-on:increment="father1"></button-counter> <button-counter v-on:incr="father2"></button-counter> <button-counter v-on:inc="father1"></button-counter> </div> <script> Vue.component('button-counter', { template: '<button v-on:click="child">{{ counter }}</button>', data: function () { return { counter: 0 } }, methods: { child:function(){ this.counter += 1; this.$emit('increment'); this.$emit('incr'); this.$emit('inc'); } } }); new Vue({ el: '#app', data: { total: 0 }, methods: { father1: function () { this.total += 1 }, father2: function () { this.total -= 1 } } }) </script> </body> </html>
點擊第一個按鈕瀏覽器的執行順序如圖所示:
步驟一:點擊第一個按鈕,執行子組件的函數child
步驟二:分別觸發該按鈕中的事件$emit(‘ ');
步驟三:監聽到 v-on:increment 事件,
步驟四:執行父組件監聽子組件的事件father1;
以上這篇對vue中v-on綁定自定事件的實例講解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com