Handling Events With Vue.js

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.

V-on

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

We can bind methods to events using their names.

<input v-model="addValue">
<button v-on:click="addToCount">Add</button>

The method addToCount specified in the template can be defined in the model as follows.

methods: {
  addToCount: function() {
    this.count = this.count + parseInt(this.addValue);
  }
}

The addToCount method will take the input from addValue and add that to the count.

Shorthand for v-on

Rather than having to declare events using the v-on: syntax, we can use the @ symbol instead:

<button @click="addToCount">Add</button>

Event Modifiers

There are frequently used calls that are made when handling events. Vue has made it easier for us to implement these by using modifiers.

For example, event.preventDefault() is often called when handling events to prevent the browsers default behavior.

Instead of having to write these out in the methods, we can use the modifiers provided with the vue-on directive.

<a href="test" @click.prevent="addToCount">Add</a>

The above code sample would remove the default behavior of the a tag and just call the addToCount method. If we didn’t add the modifier, the page would try to re-direct to the path defined in the href attribute.

The following modifiers are available in Vue.

  • stop - Prevents event bubbling up the DOM tree
  • prevent - Prevents default behavior
  • capture - Capture mode is used for event handling
  • self - Only trigger if the target of the event is itself
  • once - Run the function at most once

Key Modifiers

Similar to event modifiers, we can add key modifiers that allow us to listen to a particular key when handling key-related events such as keyup.

<input v-on:keyup.13="addToCount" v-model="addValue">

In the above example, when the keyup event is fired with the key code of 13 (the enter key), the addToCount method gets called.

Since it’s difficult to remember all of the key codes, Vue provides a set of pre-defined keys. Some examples are enter, tab, delete, esc, space and left.

Also, it’s possible to setup your own alias for key codes as follows:

Vue.config.keyCodes.a = 65
  Tweet It
✖ Clear

🕵 Search Results

🔎 Searching...