In this article we will introduce to you event processing in vuejs.
Vuejs provides us with a directive called v:on
, which can help us register and listen for dom
events, so that whenever the event is triggered, The method passed to the event will be called.
<strong>v:on</strong>
Syntax of the directive
<!-- v:on:eventname="methodname" --> <button v:on:click="handleClick">Click</button>
In the above code, we listen for ## on the button #click event so that whenever the user clicks on the button, it calls the
handleClick method.
<template> <div> <h1 id="num">{{num}}</h1> <button v-on:click="increment">Increment</button> </div> </template> <script> export default{ data:function(){ return{ num:0 } }, methods:{ increment:function(){ this.num=this.num+1 } } } </script>
How to pass parameters to event handlers?
Sometimes event handler methods can also accept parameters.<template> <div> <h1 id="num">{{num}}</h1> <!-- 参数10被传递给increment方法--> <button v-on:click="increment(10)">Increment By 10</button> </div> </template> <script> export default{ data:function(){ return{ num:0 } }, methods:{ //参数`value` increment:function(value){ this.num =this.num+value } } } </script>Here, we create an
increment method with only one parameter value to pass the parameters to the
increment(10) method.
How to access the default event object?
To access the default event object in the method vuejs, you need to provide a variable named $event.<template> <!-- 我们正在传递一个$event变量 --> <input placeholder="name" v-on:onchange="handleChange($event)" /> </template> <script> export default{ methods:{ handleChange:function($event){ console.log($event.target.value) } } } </script>Here, we access the event object by using the special $event variable provided by Vuejs. Sometimes we need to access event objects and parameters at the same time.
<template> <!-- 我们传递参数加上$event变量 --> <button v-on:click="hitMe('You hitted me',$event)"> Hit me Hard </button> </template> <script> export default{ methods:{ handleChange:function(message,$event){ $event.preventDefault() console.log(message) } } } </script>
Shorthand syntax
vuejs also provides a shorthand syntax to listen todom events.
<!--简写语法@eventname="method"--> <button @click="handleClick"></button> <!-- 长语法 --> <button v-on:click="handleClick"></button>This article is an introduction to Vue.JS event processing. I hope it will be helpful to friends in need!
The above is the detailed content of Vue.JS event handling tutorial and code examples. For more information, please follow other related articles on the PHP Chinese website!

VueUse 是 Anthony Fu 的一个开源项目,它为 Vue 开发人员提供了大量适用于 Vue 2 和 Vue 3 的基本 Composition API 实用程序函数。本篇文章就来给大家分享几个我常用的几个 VueUse 最佳组合,希望对大家有所帮助!

Vue3如何更好地使用qrcodejs生成二维码并添加文字描述?下面本篇文章给大家介绍一下Vue3+qrcodejs生成二维码并添加文字描述,希望对大家有所帮助。

本篇文章给大家整理分享8个GitHub上很棒的的 Vue 项目,都是非常棒的项目,希望当中有您想要收藏的那一个。

如何使用VueRouter4.x?下面本篇文章就来给大家分享快速上手教程,介绍一下10分钟快速上手VueRouter4.x的方法,希望对大家有所帮助!

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
