Summary of event handling in Vue.js front-end framework

Summary of event handling in Vue.js front-end framework

1. v-on event monitoring

To listen to DOM events, use the v-on directive. This directive is usually used directly in the template and executes some JavaScript code when the event is triggered.

Basic usage of the v-on directive

(1) Use the v-on directive in HTML, followed by all native event names. The basic usage is as follows:

<button v-on:click="show">Show</button>

Bind the click event to the show method. When you click the "Show" button, the show() method is executed. The show() method is defined in the Vue instance.

(2) When using the v-on directive, Vue.js provides a shorthand form of the v-on directive, “@”. The above code can be rewritten as follows:

<button @click="show">Show</button>

(3) The simple usage of the v-on directive is as follows:

<div id="box">
			<!--Basic usage of v-on -->
			<button v-on:click="count1++">Count</button>
			<p>The button was clicked {{count1}} times</p>
			<!--Simple usage of v-on-->
			<button @click="count2++">Count</button>
			<p>The button was clicked {{count2}} times</p>
</div>
		   <script type="text/javascript">
				var vm = new Vue({
					el : '#box',
					data:{
						count1:0,
						count2:0
					}
					
				});
			</script>

The simple usage of the v-on directive is shown below:

insert image description here

Event handling methods

When using the v-on directive, you need to bind an event to a method through the v-on directive, and the bound method is defined as an event handler in the methods option. The sample code is as follows:

<div id="box">
			<button v-on:click="show">Show</button>
		</div>
		   <script type="text/javascript">
				var vm = new Vue({
					el : '#box',
					data:{
						name:'Xiaoming',
						age: 29,
						occupation:'actor'
					},
					methods:{
						show:function(){
							alert('Name:'+this.name+'Age:'+this.age+'Occupation:'+this.occupation);
						}
					}
					
				});
			</script>

The result of the v-on directive binding the click event to the display method is as follows:

insert image description here

Using inline JavaScript statements

(1) In addition to binding directly to a method, the v-on directive also supports inline JavaScript statements, but only one statement can be used. The sample code is as follows:

<div id="box">
			<button v-on:click="show('Tomorrow's Star')">Show</button>
		</div>
		   <script type="text/javascript">
				var vm = new Vue({
					el : '#box',
					methods:{
						show:function(message){
							alert('Message: ' + message);
						}
					}
					
				});

The result of using inline JavaScript statements is shown below:

insert image description here

(2) When you need to obtain the native DOM event object in an inline statement, you can pass a special variable $event into the method. The sample code is as follows:

<div id="box">
			<a href="http://www.baidu.com" rel="external nofollow" v-on:click="show('Tomorrow's Star',$event)">{{message}}</a>
		</div>
		   <script type="text/javascript">
				var vm = new Vue({
					el : '#box',
					data:{
						message:'Hello'
					},
					methods:{
						show:function(message1,e){
							e.preventDefault();
							alert(message1);
						}
					}
					
				});
			</script>

In addition to passing a value to the show() method, a special variable $event is also passed. The function of this variable is to process the native DOM event when the hyperlink is clicked, and apply the preventDefault() method to prevent the hyperlink from jumping. When you click the "Hello" hyperlink, a dialog box will pop up and the running result is as shown in the figure below:

insert image description here

2. Event handling modifiers

The so-called modifier is a special suffix indicated by a half-width period. Vue.js provides multiple modifiers for the v-on directive, including event modifiers and key modifiers.
Event modifiers <br /> In event handlers, preventDefault() or stopPropagation() methods are often called to implement specific functions. To handle these DOM event details, Vue.js provides event modifiers for the v-on directive. Commonly used event modifiers and their descriptions are shown in the following table:

insert image description here

Modifiers can be used in series, and you can use only modifiers without binding event handling methods. Event modifiers are used as follows:

<!-- Prevent the click event from propagating further -->
<a v-on:click.stop="doSomething"></a>
<!-- Prevent the form from submitting by default -->
<form v-on:submit.prevent="onSubmit"></form>
<!-- The handler function is called only when the event is triggered from the current element itself -->
<div v-on:click.self="doSomething"></div>
<!-- Modifier chaining, preventing the form from submitting by default and preventing bubbling-->
<a v-on:click.stop.prevent="doSomething"></a>
<!-- Only modifiers, no event binding -->
<form v-on:submit.prevent></form>

The following is a sample code that applies a .stop modifier to stop event bubbling:

<div id="box">
			<div v-on:click="show('div event trigger')">
				<button v-on:click.stop="show('button event trigger')">Show</button>
			</div>
		</div>
		   <script type="text/javascript">
				var vm = new Vue({
					el : '#box',
					methods:{
						show:function(message){
							alert(message);
						}
					}
					
				});
			</script>

When you click the "Show" button, only the click event of that button will be triggered. If the .stop modifier is not used in the button, when the "Show" button is clicked, not only the click event of the button will be triggered, but also the click event of the div will be triggered, and two dialog boxes will pop up. The running results are shown in the figure below:
(1) Use the .stop modifier:

insert image description here

(2) Click OK without using the .stop modifier:

insert image description here
insert image description here

Key Modifiers

In addition to event modifiers, Vue.js also provides key modifiers for the v-on directive to listen for key presses in keyboard events. When a keyboard event is triggered, the keyCode value of the key needs to be detected. The code is as follows:

<input v-on:keyup.13="submit">

Apply the v-on directive to monitor the keyboard's keyup event. The keyCode value of the Enter key on the keyboard is 13. Therefore, after entering content in the text box, the submit() method will be called when the Enter key is clicked.
For the code shown above, you can use its alias. The Enter key alias is Enter. The code is as follows:

<input v-on:keyup.enter="submit">

The aliases provided by Vue.js for commonly used buttons are shown in the following table:

insert image description here

Note: We will continue to follow up on the Vue.js front-end framework: form control binding in the future. I hope you will support and pay attention to it.

This concludes this article about the event handling summary of the Vue.js front-end framework. For more relevant Vue.js event handling content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of Vue's click event anti-shake and throttling processing
  • Detailed explanation of Vue event handling principle and process
  • Detailed explanation of Vue event handling
  • Vue3 Vue Event Handling Guide
  • Detailed explanation of Vue event handling and event modifiers
  • Introducing mousewheel events and compatibility processing in Vue
  • Details of event handling in Vue

<<:  View the dependent libraries of so or executable programs under linux

>>:  Solution to forgetting the MYSQL database password under MAC

Recommend

React internationalization react-intl usage

How to achieve internationalization in React? The...

mysql5.7.20 installation and configuration method graphic tutorial (mac)

MySQL 5.7.20 installation and configuration metho...

Detailed explanation of vue page state persistence

Table of contents Code: Replenish: Summarize Requ...

SQL insert into statement writing method explanation

Method 1: INSERT INTO t1(field1,field2) VALUE(v00...

Solve the problem of docker pull being reset

This article introduces how to solve the problem ...

uniapp implements date and time picker

This article example shares the specific code of ...

Share the pitfalls of MySQL's current_timestamp and their solutions

Table of contents MySQL's current_timestamp p...

Detailed tutorial on Docker pulling Oracle 11g image configuration

Without further ado Start recording docker pullin...

Mysql 5.7.18 Using MySQL proxies_priv to implement similar user group management

Use MySQL proxies_priv (simulated role) to implem...

Case study of dynamic data binding of this.$set in Vue

I feel that the explanation of this.$set on the I...

Complete steps to use vue-router in vue3

Preface Managing routing is an essential feature ...

CentOS7 deploys version 19 of docker (simple, you can follow it)

1. Install dependency packages [root@localhost ~]...

Using puppeteer to implement webpage screenshot function on linux (centos)

You may encounter the following problems when ins...

How to completely delete the MySQL 8.0 service under Linux

Before reading this article, it is best to have a...

Solve the mobile terminal jump problem (CSS transition, target pseudo-class)

Preface Many friends who have just come into cont...