1. Listening for events You can use The sample code is as follows: <div id="app"> <p>{{counter}}</p> <button @click="counter += 1">+1</button> <button @click="subtract(10)">-10</button> </div> <script> const app = new Vue({ el: "#app", data: { counter: 0 }, methods: { subtract(value){ this.counter -= value } } }) </script> 2. Pass in event parameters If you want to get the native The sample code is as follows: <button v-on:click="subtract(10,$event)">Subtract 10</button> ... <script> ... methods: { subtract: function(value,event){ this.count -= value; console.log(event); } } ... </script> 3. Event modifiers It is a very common requirement to call To solve this problem,
Case 1: Prevent click events from propagating further<div id="app"> <div @click="divClick"> 1111 <button @click.stop="btnClick">Button</button> </div> </div> <script> let app = new Vue({ el: "#app", data: { count: 0 }, methods: { divClick(){ console.log("divClick") }, btnClick(){ console.log("btnClick") } } }); </script> Case 2: Submit event no longer reloads the page<div id="app"> <form action=""> <label> <input type="text"> </label> <label> <input type="submit" value="Submit"> </label> </form> </div> <script> const app = new Vue({ el: "#app", data: { } }) </script> The above is the most standard code for submitting data. After submission, it will automatically jump to Baidu. However, there is a requirement now. We hope that after entering the data, it will not automatically jump to Baidu, but process the data by our own method first, and then jump to the specified page after processing. The code is as follows: <div id="app"> <form action="https://www.baidu.com"> <label> <input type="text"> </label> <label> <input type="submit" value="Submit" @click.prevent="testClick"> </label> </form> </div> <script> const app = new Vue({ el: "#app", methods: { testClick(){ } } }) </script> Here we bind a click event to This is the end of this article about the use of Vue v-on directive. For more relevant Vue v-on directive content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: In-depth explanation of hidden fields, a new feature of MySQL 8.0
>>: Web page HTML code: production of scrolling text
What is em? em refers to the font height, and the ...
Many web designers are confused about the width of...
Using provide+inject combination in Vue First you...
Table of contents vue custom directive Global Dir...
The img tag in XHTML is so-called self-closing, w...
How to change the password in MySQL 5.7.18: 1. Fi...
Maybe everyone knows that js execution will block...
1. Add the plug-in and add the following configur...
Table of contents Preface What is data binding? T...
HTML is a hybrid language used for publishing on ...
When making a web page, we usually need to consid...
0. Overview Zabbix is an extremely powerful ope...
Table of contents 1. Maven Dependency 2. Menu rel...
The table is as follows: HTML source code Display...
SSH public key authentication is one of the SSH a...