Vue.JS event handling tutorial and code examples
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>{{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>{{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>
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>
<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>
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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When using the Vue framework to develop front-end projects, we will deploy multiple environments when deploying. Often the interface domain names called by development, testing and online environments are different. How can we make the distinction? That is using environment variables and patterns.

Ace is an embeddable code editor written in JavaScript. It matches the functionality and performance of native editors like Sublime, Vim, and TextMate. It can be easily embedded into any web page and JavaScript application. Ace is maintained as the main editor for the Cloud9 IDE and is the successor to the Mozilla Skywriter (Bespin) project.

Foreword: In the development of vue3, reactive provides a method to implement responsive data. This is a frequently used API in daily development. In this article, the author will explore its internal operating mechanism.

Vue.js has become a very popular framework in front-end development today. As Vue.js continues to evolve, unit testing is becoming more and more important. Today we’ll explore how to write unit tests in Vue.js 3 and provide some best practices and common problems and solutions.

In Vue.js, developers can use two different syntaxes to create user interfaces: JSX syntax and template syntax. Both syntaxes have their own advantages and disadvantages. Let’s discuss their differences, advantages and disadvantages.

In the actual development project process, sometimes it is necessary to upload relatively large files, and then the upload will be relatively slow, so the background may require the front-end to upload file slices. It is very simple. For example, 1 A gigabyte file stream is cut into several small file streams, and then the interface is requested to deliver the small file streams respectively.

Since the release of Vue3, the word composition API has entered the field of vision of students who write Vue. I believe everyone has always heard how much better the composition API is than the previous options API. Now, due to the release of the @vue/composition-api plug-in, Vue2 Students can also get on the bus. Next, we will mainly use responsive ref and reactive to conduct an in-depth analysis of how this plug-in achieves this.

When I was working on the chatgpt mirror site, I found that some mirror sites did not have typewriter cursor effects, but only text output. Did they not want to do it? I want to do it anyway. So I studied it carefully and realized the effect of typewriter plus cursor. Now I will share my solution and renderings~
