回顾
vue中绑定事件的基本语法之总结:
- 在vue中绑定事件是通过v-on指令来完成的,语法为----v-on:事件名
- 在v-on:事件名的赋值语句中是当前事件触发调用的函数名
- 在v-on中事件的函数统一定义在vue实例的methods属性中
- 在vue定义的事件中this指的就是当前vue实例,日后可以通过this获取实例中的相关数据或调用methods中相关方法
一、vue事件参数传递
<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h1>{{msg}}</h1>
<h1>{{count}}</h1>
<button v-on:click="incrementAge1">点击每次年龄+1</button>
<!-- 注意传参 -->
<button v-on:click="changeAge10(10)">点击每次年龄+10</button>
<!-- 当参数多时,参数可以设置为对象 -->
<button v-on:click="test({count:1,msg:'hello'})">点击每次年龄+1,同时msg+"hello!"</button>
</div>
</body>
</html>
<script src="js/vue.js"></script>
<script>
new Vue({
el: "#app",
data: {
msg: "悬悬小",
count:0
},
methods: {
incrementAge1:function () {
this.count++;
},
changeAge10: function (count) {
// console.log(this.count);
this.count+=count;
},
test: function (param) {
// console.log(this.msg);
//注意取参的方式
this.count+=param.count;
this.msg +=param.msg;
}
}
});
</script>
<!--需求:
定义三个按钮
1.点击每次年龄加1
2.点击每次年龄加10
3.点击每次年龄加1,同时msg+“你好!”
-->
二、vue事件的简化语法
- 通过 @事件名 = 事件处理函数名 简化v-on事件绑定
- 通过 函数名(){} 简化函数的书写
<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<h1>{{msg}}</h1>
<h1>{{count}}</h1>
<!-- v-on指令的简化写法 @事件名=事件处理函数名 -->
<button @click="incrementAge1">点击每次年龄+1</button>
<button @click="incrementAge2(2)">点击每次年龄+2</button>
</div>
</body>
</html>
<script src="js/vue.js"></script>
<script>
new Vue({
el: "#app",
data: {
msg: "悬悬小",
count: 0
},
methods: {
incrementAge1:function () {
this.count++;
},
//简化函数的写法 函数名(){}
incrementAge2(count){
this.count +=count;
}
}
});
</script>
总结
以上就是vue中事件绑定语法的全部知识。