添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Tutorial

Handling Events With Vue.js

Published on January 2, 2017
author

Alligator.io

Handling Events With Vue.js

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Vue.js allows us to handle events triggered by the user. Handling events helps add interactivity to web apps by responding to the user’s input. Vue provides us with the v-on directive to handle these events.

User interactions with the view can trigger events on the DOM such as click and keyup . We can use the v-on directive to handle these events.

As a simple example, we can implement a counter that increments every time a user clicks a button. Let’s start off by initializing a counter to zero in the data model:

data() {
  return {
    count: 0

In the view, we can define the method to run when a button is clicked:

<label>Count is: {{count}}</label>
<button v-on:click="count++">Increment</button>

v-on will trigger the expression or the method specified when the click event in triggered on the button element.

In this example, it would increment the count value by one.

Binding methods to v-on

Shorthand for v-on

Event Modifiers

Key Modifiers