关于官网vue中v-on绑定自定义事件的个人理解
对官网实例进行了一些修改,如下图:
<!DOCTYPE html>
<html lang="en">
<head>
< charset="UTF-8">
< >v-on绑定自定义事件</ >
< src="vue.js"></ >
</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>
< >
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
}
}
})
</ >
</body>
</html>

点击第一个按钮浏览器的执行顺序如图所示:
步骤一:点击第一个按钮,执行子组件的函数child
步骤二:分别触发该按钮中的事件$emit(‘ ');
步骤三:监听到 v-on:increment 事件,
步骤四:执行父组件监听子组件的事件father1;
以上这篇对vue中v-on绑定自定事件的实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
-
8 分钟了解 Kubernetes
2026-05-18栏目: 教程
-
Helm 从入门到实践 | 从 0 开始制作一个 Helm Charts
2026-05-18栏目: 教程
-
阿里云突发性能实例t5 和共享型实例xn4 n4的区别
2026-05-18栏目: 教程
-
【DockerCon2017技术解读】Docker特性介绍
2026-05-18栏目: 教程
-
面向海量数据的极致成本优化-云HBase的一体化冷热分离
2026-05-18栏目: 教程
