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:

<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:

var vm = new Vue({
  el: '#example',
  data: {
    name: 'Vue.js'
  },
  // 在 `methods` 对象中定义方法
  methods: {
    greet: function (event) {
      // 方法内 `this` 指向 vm
      alert('Hello ' + this.name + '!')
      // `event` 是原生 DOM 事件
      alert(event.target.tagName)
    }
  }
})

// 也可以在 JavaScript 代码中调用方法
vm.greet() // -> 'Hello Vue.js!'
Copy after login

Inline statement processor

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

<p id="example-2">
  <button v-on:click="say('hi')">Say Hi</button>
  <button v-on:click="say('what')">Say What</button>
</p>

new Vue({
  el: '#example-2',
  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:

<button v-on:click="say('hello!', $event)">Submit</button>
Copy after login
// ...
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?

<!-- 阻止单击事件冒泡 -->
<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:

<!-- 添加事件侦听器时使用 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:

<!-- 只有在 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:

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

<!-- 缩写语法 -->
<input @keyup.enter="submit">
Copy after login

All key aliases:

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:

// 可以使用 @keyup.f1
Vue.directive('on').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 Article Tags

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

The operation process of WIN10 service host occupying too much CPU

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

How to solve the problem that jQuery cannot obtain the form element value

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

Learn how to handle special characters and convert single quotes in PHP

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 in the Rulong 8 Wine Master exam?

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

How to implement change event binding of select elements in jQuery

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

PHP string processing: How to remove the first character on the right?

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

What happens if there is a problem with the sound card driver?

Analysis of common problems when Linux SSH service fails to start Analysis of common problems when Linux SSH service fails to start Mar 19, 2024 pm 05:48 PM

Analysis of common problems when Linux SSH service fails to start

See all articles