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.
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.
Rather than having to declare events using the v-on: syntax, we can use the @ symbol instead:
<button @click="addToCount">Add</button>
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.
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.
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.
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!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.