vue事件监听 v-on

在前端开发中,我们经常用到交互,在vue中如何监听事件呢,使用v-on

v-on介绍

作用:绑定事件监听器

缩写:@

参数:event

1、v-on基础用法

<button @click="increment">+</button>
<button @click="decrement">-</button>

const app = new Vue({
  el: "#app", //用于挂载要管理的元素
  data: {   //定义数据
    message: 'hello',
    counter: 0
  },
  methods: {
    increment(){
      this.counter++;
    },
    decrement(){
      this.counter--;
    }
  }
})
 
 
 
 

2、v-on 的参数问题

1、事件调用的方法没有参数

<button @click="btn1click()">按钮1</button>
<button @click="btn1click">按钮1</button>

btn1click(){
  console.log("btn1click");
}
 
 

2、在事件定义时,写函数时省略了小括号,但是方法本身是需要一个参数的,这个时候,Vue

会默认将浏览器产生的event事件对象作为参数传递到方法中

<button @click="btn2click">按钮2</button>

btn2click(abc){
  console.log('--------',abc);
}

clipboard

 
 

3、方法定义时,我们需要event对象,同时也需要其他参数

在调用方法时,如何手动获取浏览器参数的event对象:$event

<button @click="btn3click(123,$event)">按钮3</button>

btn3click(abc,event){
  console.log('---++-----',abc,event);
}

clipboard

 
 

3、v-on修饰符的使用

1)stop修饰符的使用

阻止事件冒泡

<div @click="divclick">
  <button @click.stop="btnclick">点击我</button>
</div>

2)prevent修饰符的使用

阻止默认事件

<form action="baidu">
  <input type="submit" value="提交" @click.prevent="submitclick">
</form>

3)监听某个键盘的键帽

<input type="text" @keyup.enter="keyup"> //监听enter键

4)once修饰符的使用

<button @click="btn2click.once">按钮2</button>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值