Vue.js 2 Events
Vue.js allows us to intercept any DOM event by using the v-on directive on an element. This topic is key to making a component interactive
What are Vue.js events
Vue.js allows us to intercept any DOM event by using the v-on directive on an element.
If we want to do something when a click event happens in this element:
<template>
<a>Click me!</a>
</template>
we add a v-on directive:
<template>
<a v-on:click="handleClick">Click me!</a>
</template>
Vue also offers a very convenient alternative syntax for this:
<template>
<a @click="handleClick">Click me!</a>
</template>
You can choose to use the parentheses or not. @click="handleClick" is equivalent to @click="handleClick()".
handleClick is a method attached to the component:
<script>
export default {
methods: {
handleClick: function(event) {
console.log(event)
}
}
}
</script>
Methods are explained more in detail in my Vue Methods tutorial.
What you need to know here is that you can pass parameters from events: @click="handleClick(param)" and they will be received inside the method.
Access the original event object
In many cases, you will want to perform an action on the event object or look up some property in it. How can you access it?
Use the special $event directive:
<template>
<a @click="handleClick($event)">Click me!</a>
</template>
<script>
export default {
methods: {
handleClick: function(event) {
console.log(event)
}
}
}
</script>
and if you already pass a variable:
<template>
<a @click="handleClick('something', $event)">Click me!</a>
</template>
<script>
export default {
methods: {
handleClick: function(text, event) {
console.log(text)
console.log(event)
}
}
}
</script>
From there you could call event.preventDefault(), but there’s a better way: event modifiers
Event modifiers
Instead of messing with DOM “things” in your methods, tell Vue to handle things for you:
@click.preventcallevent.preventDefault()@click.stopcallevent.stopPropagation()@click.passivemakes use of the passive option of addEventListener@click.captureuses event capturing instead of event bubbling@click.selfmake sure the click event was not bubbled from a child event, but directly happened on that element@click.oncethe event will only be triggered exactly once
All those options can be combined by appending on modifier after the other.
For more on propagation, bubbling/capturing see my JavaScript events guide.
download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.