About the use of Vue v-on directive

About the use of Vue v-on directive

1. Listening for events

You can use v-on directive to listen for DOM events and run some JavaScript code when they are triggered. The event code can be placed directly after v-on or written as a function.

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 DOM event in the event handling function, you can pass a $event parameter when calling it in the html code.

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 event.preventDefault() or event.stopPropagation() in an event handler. Although we can easily do this in the method, it is better to have the method have pure data logic instead of handling DOM event details.

To solve this problem, Vue.js provides event modifiers for v-on . As mentioned before, modifiers are indicated by a dot at the beginning of the command suffix.

  • .stop: event.stopPropagation , stops event bubbling.
  • .prevent: event.preventDefault , prevent the default behavior
  • .capture: event capture.
  • .self: represents the currently clicked element itself.
  • .once: This event is executed only once.
  • .passive: Tells the browser not to prevent the default behavior when scrolling the page, making the scrolling smoother.

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 submit and use .prevent to prevent its default behavior.

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:
  • Vue directive v-html uses filters function example
  • Vue v-text directive simple usage example
  • Vue.js instruction v-for usage and subscript index acquisition
  • A brief discussion on the use of v-on event instructions in Vue.js
  • v-cloak directive in Vue.js and detailed usage
  • Vue.js instruction v-for usage and index acquisition
  • Parsing the commonly used v-instructions in vue.js

<<:  In-depth explanation of hidden fields, a new feature of MySQL 8.0

>>:  Web page HTML code: production of scrolling text

Recommend

Linux system AutoFs automatic mount service installation and configuration

Table of contents Preface 1. Install the service ...

Basic implementation method of cross-component binding using v-model in Vue

Hello everyone, today we will talk about how to u...

Detailed installation and configuration of Subversion (SVN) under Ubuntu

If you are a software developer, you must be fami...

Will mysql's in invalidate the index?

Will mysql's IN invalidate the index? Won'...

Implementing custom radio and check box functions with pure CSS

1. Achieve the effect 2 Knowledge Points 2.1 <...

Example code of vue icon selector

Source: http://www.ruoyi.vip/ import Vue from ...

How to build a redis cluster using docker

Table of contents 1. Create a redis docker base i...

How to add fields to a large data table in MySQL

Preface I believe everyone is familiar with addin...

CSS to achieve Cyberpunk 2077 style visual effects in a few steps

background Before starting the article, let’s bri...

CSS animation combined with SVG to create energy flow effect

The final effect is as follows: The animation is ...

MySQL Database Iron Laws (Summary)

Good database specifications help reduce the comp...

Introduction to Linux File Compression and Packaging

1. Introduction to compression and packaging Comm...

Examples of using MySQL covering indexes

What is a covering index? Creating an index that ...

JavaScript to implement drop-down list selection box

This article example shares the specific code of ...

Win2008 R2 mysql 5.5 zip format mysql installation and configuration

Win2008 R2 zip format mysql installation and conf...