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

Solution for front-end browser font size less than 12px

Preface When I was working on a project recently,...

A brief analysis of how to use border and display attributes in CSS

Introduction to border properties border property...

mysql query data for today, this week, this month, and last month

today select * from table name where to_days(time...

Detailed explanation of the execution process of MySQL query statements

Table of contents 1. Communication method between...

Compile CPP files using G++ in Ubuntu

When I used g++ to compile the cpp file for the f...

Summary of the three stages of visual designer growth

Many people have read this book: "Grow as a ...

Implementation of CSS loading effect Pac-Man

emmm the name is just a random guess 2333 Preface...

Form submission refresh page does not jump source code design

1. Design source code Copy code The code is as fol...

HTML table tag tutorial (17): table title vertical alignment attribute VALIGN

The table caption can be placed above or below th...

Screen command and usage in Linux

Screen Introduction Screen is a free software dev...

How to install MySQL 5.7 from source code in CentOS 7 environment

This article describes how to install MySQL 5.7 f...

CSS container background 10 color gradient Demo (linear-gradient())

grammar background: linear-gradient(direction,col...