Table of Contents
Method and event processor
Method processor
Inline statement processor
Event modifier
Key modifiers
Why listen for events in HTML?
Home Web Front-end JS Tutorial Issues with Vue methods and event handling

Issues with Vue methods and event handling

Oct 24, 2017 am 10:54 AM
event deal with question

Method and event processor

Method processor

You can use the v-on command to monitor DOM events:

1

2

3

<p id="example">

  <button v-on:click="greet">Greet</button>

</p>

Copy after login

We are bound A click event handler to a method greet. Define this method in the Vue instance below:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

var vm = new Vue({

  el: &#39;#example&#39;,

  data: {

    name: &#39;Vue.js&#39;

  },

  // 在 `methods` 对象中定义方法

  methods: {

    greet: function (event) {

      // 方法内 `this` 指向 vm

      alert(&#39;Hello &#39; + this.name + &#39;!&#39;)

      // `event` 是原生 DOM 事件

      alert(event.target.tagName)

    }

  }

})

 

// 也可以在 JavaScript 代码中调用方法

vm.greet() // -> &#39;Hello Vue.js!&#39;

Copy after login

Inline statement processor

In addition to directly binding to a method, you can also use inline JavaScript statements:

1

2

3

4

5

6

7

8

9

10

11

12

13

<p id="example-2">

  <button v-on:click="say(&#39;hi&#39;)">Say Hi</button>

  <button v-on:click="say(&#39;what&#39;)">Say What</button>

</p>

 

new Vue({

  el: &#39;#example-2&#39;,

  methods: {

    say: function (msg) {

      alert(msg)

    }

  }

}

Copy after login

Similar to inline expressions, event handlers are limited to one statement.

Sometimes it is also necessary to access native DOM events in an inline statement processor. You can use the special variable $event to pass it into the method:

1

<button v-on:click="say(&#39;hello!&#39;, $event)">Submit</button>

Copy after login

1

2

3

4

5

6

7

// ...

methods: {

  say: function (msg, event) {

    // 现在我们可以访问原生事件对象

    event.preventDefault()

  }

}

Copy after login

Event modifier

often needs to be called in the event handler event.preventDefault() or event.stopPropagation(). Although we can easily do this within a method, it would be better to have the method be pure data logic and not deal with DOM event details.

In order to solve this problem, Vue.js provides two event modifiers for v-on: .prevent and .stop. Do you still remember that modifiers are command suffixes starting with a period?

1

2

3

4

5

6

7

8

9

10

11

<!-- 阻止单击事件冒泡 -->

<a v-on:click.stop="doThis"></a>

 

<!-- 提交事件不再重载页面 -->

<form v-on:submit.prevent="onSubmit"></form>

 

<!-- 修饰符可以串联 -->

<a v-on:click.stop.prevent="doThat">

 

<!-- 只有修饰符 -->

<form v-on:submit.prevent></form>

Copy after login

1.0.16 Added two additional modifiers:

1

2

3

4

5

<!-- 添加事件侦听器时使用 capture 模式 -->

<p v-on:click.capture="doThis">...</p>

 

<!-- 只当事件在该元素本身(而不是子元素)触发时触发回调 -->

<p v-on:click.self="doThat">...</p>

Copy after login

Key modifiers

When listening for keyboard events, we often need to detect keyCode. Vue.js allows adding key modifiers for v-on:

1

2

<!-- 只有在 keyCode 是 13 时调用 vm.submit() -->

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

Copy after login

It is difficult to remember all the keyCode, Vue.js provides aliases for the most commonly used keys:

1

2

3

4

5

<!-- 同上 -->

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

 

<!-- 缩写语法 -->

<input @keyup.enter="submit">

Copy after login

All key aliases:

1

2

3

4

5

6

7

8

9

enter

tab

delete

esc

space

up

down

left

right

Copy after login

1.0.8+: Supports single-letter key aliases.

1.0.17+: You can customize the key alias:

1

2

// 可以使用 @keyup.f1

Vue.directive(&#39;on&#39;).keyCodes.f1 = 112

Copy after login

Why listen for events in HTML?

You may notice that this way of event monitoring goes against the tradition Concept "separation of concern". Don't worry, since all Vue.js event handlers and expressions are strictly bound to the current view's ViewModel, it won't cause any maintenance difficulties. In fact, using v-on has several advantages:

  1. You can easily locate the corresponding method in the JavaScript code by scanning the HTML template.

  2. Because you don’t need to manually bind events in JavaScript, your ViewModel code can be very pure logic, completely decoupled from the DOM, and easier to test.

  3. When a ViewModel is destroyed, all event handlers will be automatically deleted. You don’t have to worry about cleaning them yourself.

The above is the detailed content of Issues with Vue methods and event handling. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The operation process of WIN10 service host occupying too much CPU The operation process of WIN10 service host occupying too much CPU Mar 27, 2024 pm 02:41 PM

1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

Learn how to handle special characters and convert single quotes in PHP Learn how to handle special characters and convert single quotes in PHP Mar 27, 2024 pm 12:39 PM

In the process of PHP development, dealing with special characters is a common problem, especially in string processing, special characters are often escaped. Among them, converting special characters into single quotes is a relatively common requirement, because in PHP, single quotes are a common way to wrap strings. In this article, we will explain how to handle special character conversion single quotes in PHP and provide specific code examples. In PHP, special characters include but are not limited to single quotes ('), double quotes ("), backslash (), etc. In strings

How to solve the problem that jQuery cannot obtain the form element value How to solve the problem that jQuery cannot obtain the form element value Feb 19, 2024 pm 02:01 PM

To solve the problem that jQuery.val() cannot be used, specific code examples are required. For front-end developers, using jQuery is one of the common operations. Among them, using the .val() method to get or set the value of a form element is a very common operation. However, in some specific cases, the problem of not being able to use the .val() method may arise. This article will introduce some common situations and solutions, and provide specific code examples. Problem Description When using jQuery to develop front-end pages, sometimes you will encounter

What are the questions in the Rulong 8 Wine Master exam? What are the questions in the Rulong 8 Wine Master exam? Feb 02, 2024 am 10:18 AM

What are the questions involved in the Yulong 8 Wine Master exam? What is the corresponding answer? How to pass the exam quickly? There are many questions that need to be answered in the Master of Wine Examination activities, and we can refer to the answers to solve them. These questions all involve knowledge of wine. If you need a reference, let’s take a look at the detailed analysis of the answers to the Yakuza 8 Wine Master exam questions! Detailed explanation of answers to questions in the Rulong 8 Wine Master exam 1. Questions about "wine". This is a distilled liquor produced by a distillery established by the royal family. It is brewed from the sugar of sugarcane grown in large quantities in Hawaii. What is the name of this wine? Answer: Rum 2. Question about "wine". The picture shows a drink made from dry ginseng and dry vermouth. It is characterized by the addition of olives and is known as "cockney"

How to implement change event binding of select elements in jQuery How to implement change event binding of select elements in jQuery Feb 23, 2024 pm 01:12 PM

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

PHP programming tips: How to deal with the last semicolon situation PHP programming tips: How to deal with the last semicolon situation Mar 26, 2024 pm 12:45 PM

PHP Programming Tips: How to Handle the Last Semicolon In PHP programming, you often encounter situations where you need to handle the last semicolon. Especially in loops and conditional statements, it is easy to cause program errors by writing one less or one more semicolon. In order to avoid this situation, we can adopt some programming techniques to handle the last semicolon situation. The following are some common techniques and code examples for handling the last semicolon: 1. Use if statements to determine the last semicolon

What happens if there is a problem with the sound card driver? What happens if there is a problem with the sound card driver? Mar 02, 2024 am 10:49 AM

The sound card driver is a system program in the computer that controls and directs the sound card. It can help us play sound. Therefore, if there is a problem with the sound card driver, the most intuitive situation is that in terms of sound, there will be no sound or the sound will fluctuate and freeze abnormally. What will happen if there is a problem with the sound card driver: 1. Sound error 1. The sound card driver serves the sound, so the most intuitive problem is the sound problem. 2. Whether there is no sound from the computer, or the sound is stuck, delayed, noisy, or the volume tone is abnormal, it may be related to the sound card driver. 3. So when we encounter similar problems, we can try reinstalling or updating the sound card driver. 2. Exclamation mark in Device Manager 1. If there is no problem with the sound, it means that the sound card driver is normal in most cases. 2. But I

PHP string processing: How to remove the first character on the right? PHP string processing: How to remove the first character on the right? Mar 01, 2024 pm 12:51 PM

Processing strings in PHP is a very common operation, and removing the first character on the right is also a common need. In this article, I will show you how to remove the first character on the right using PHP code. First, let's look at a simple example of a string processing function that demonstrates how to remove the first character on the right:

See all articles