這篇文章將為大家詳細講解有關如何實現對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綁定自定事件”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。