Tutorial

Handling Events With Vue.js

Published on January 2, 2017
Default avatar

By 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.

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

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Alligator.io

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel