Let's talk about the v-on parameter problem in Vue

Let's talk about the v-on parameter problem in Vue

Use of v-on:clock in Vue

I'm currently learning the vue.js framework. Make a note of any problems you encounter so you can refer to them later.

First, here is a page (for easier observation, each label is colored one by one):

The html code is:

<div class="groupbody">
   <ul class="list">
     <li v-for="cell in todo.groupbody " class="pagegroupcell " v-on:click="exchange($event)">
       <div class="pagecelltext">{{ cell.left }}</div>
       <div class="pagecellmin">{{ cell.min }}</div>
       <img src="img/images/direction/right.png" style="width: 20px;height: 30px;float: right;margin-top: 5px;">
       <div class="pagecellmsg">{{ cell.right }}</div> 
      </li>
    </ul>
    <div class="clear"></div>
</div>

js part of the code:

exchange: function (event) {
            alert("Start executing method");
            var a = event.target;
            var cellimg = a.getElementsByTagName("div")[0];
            var msg = cellimg.innerText; 
            page2datas.todos[0].groupheader = msg;
            alert("Method is executing"); 
            var b = document.getElementById("page1");
            b.style.display = "none";
            var c = document.getElementById("page2");
            c.style.display = "block";
            alert("Method execution ended");
          }

At this time, if you click the colored area in the cell (that is, when you click the text label of the li tag), only the first alert() method is executed, and the following two methods are not executed.

The reason is the event parameter of this method:

If there is a parameter $event in the tag binding method, you can use event.target to get the tag that is bound to the click event at the current point.

However, this parameter may also cause some problems. For example, if you want to execute the click event method completely regardless of which part of the li tag is clicked, the event parameter is not applicable. At this time, we can only think of other solutions.

Solution:

There is a v-for loop in li, which has a cell object, which contains the data in the li tag. You only need to pass this object to the click event method, and you can use this object to achieve the purpose you just wanted to achieve.

The modified html code:

<div class="groupbody">
   <ul class="list">
     <li v-for="cell in todo.groupbody " class="pagegroupcell " v-on:click="exchange(cell)">
       <div class="pagecelltext">{{ cell.left }}</div>
       <div class="pagecellmin">{{ cell.min }}</div>
       <img src="img/images/direction/right.png" style="width: 20px;height: 30px;float: right;margin-top: 5px;">
       <div class="pagecellmsg">{{ cell.right }}</div> 
      </li>
    </ul>
    <div class="clear"></div>
</div>

The modified js code:

 exchange: function (cell) {
        alert("Start executing method"); 
        page2datas.todos[0].groupheader =cell.left;
        alert("Executing");
        var b = document.getElementById("page1");
        b.style.display = "none";
        var c = document.getElementById("page2");
        c.style.display = "block";
        alert("Method execution ended");
      }

At this point, the entire electric shock event execution method can be fully executed, and subsequent page switching can also be completed.

Supplement: Parameter issues when binding click events to v-on in Vue

There are three cases regarding parameters when binding click events to v-on, as follows:

There are no parentheses after the bound method

 <button @click="btnClick">Click</button>
 <script>
 const app = new Vue({
  methods:{
  btnClick(event){
  // At this time, event is the object currently clicked console.log(event)
  }
  }
 })
 </script>

The bound method is followed by parentheses

<button @click="btnClick()">Click</button>
<script>
 const app = new Vue({
 methods:{
  btnClick(event){
  // At this time event is undefined
  console.log(event)
  }
 }
 })
</script>

The bound method has parentheses after it, and parameters need to be passed

<button @click="btnClick(123)">Click</button>
<script>
 const app = new Vue({
 methods:{
  btnClick(event){
  // At this time, event is 123
  console.log(event)
  }
 }
 })
</script>

The bound method has parentheses after it, and it needs to pass parameters and the currently clicked object.

<!-- In this case, if you need to pass the currently clicked object, the parameter must be $event -->
<!-- If you want to pass a number in the first position, you don't need to add quotation marks. If you want to pass a string, you must add quotation marks. If you don't add quotation marks, Vue will parse it as a variable. If it is not defined in data, an error will be reported. -->
<button @click="btnClick(123,$event)">Click</button>
<script>
 const app = new Vue({
 methods:{
  btnClick(num,event){
  // At this time, num is 123, event is the object currently clicked,
  console.log(num,event)
  }
 }
 })
</script>

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Steps to pass dynamic parameters in vue v-on:click
  • Detailed explanation of the basic usage of v-on event monitoring instructions in Vue
  • A brief discussion on the use of v-on event instructions in Vue.js

<<:  Use iptables and firewalld tools to manage Linux firewall connection rules

>>:  Problems encountered by MySQL nested transactions

Recommend

How to query the intersection of time periods in Mysql

Mysql query time period intersection Usage scenar...

Explanation of Dockerfile instructions and basic structure

Using Dockerfile allows users to create custom im...

How to recover accidentally deleted messages files in Linux

If there are files that are being used by a proce...

Detailed explanation of sshd service and service management commands under Linux

sshd SSH is the abbreviation of Secure Shell, whi...

How to avoid data loop conflicts when MySQL is configured with dual masters

I wonder if you have ever thought about this ques...

Detailed explanation of the flexible use of CSS grid system in projects

Preface CSS grids are usually bundled in various ...

How to set up URL link in Nginx server

For websites with an architecture like LNMP, they...

Using Vue to implement timer function

This article example shares the specific code of ...

CSS Paint API: A CSS-like Drawing Board

1. Use Canvas images as CSS background images The...

Example code of the spread operator and its application in JavaScript

The spread operator allows an expression to be ex...

Detailed discussion on the issue of mysqldump data export

1. An error (1064) is reported when using mysqldu...

vue.config.js packaging optimization configuration

The information on Baidu is so diverse that it...

How to handle MySQL numeric type overflow

Now, let me ask you a question. What happens when...