vuejs
/

Vue v-on Directive

Last Sync: Today

On this page

12
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

vuejs

Vue v-on Directive

What is v-on?

The v-on directive is used to listen to DOM events and execute methods when those events are triggered.

Basic Syntax

HTMLRead-only
1
<button v-on:click="handleClick">Click Me</button>

Shorthand Syntax

HTMLRead-only
1
<button @click="handleClick">Click Me</button>

Inline Event Handling

HTMLRead-only
1
<button @click="count++">Increment</button>
<p>{{ count }}</p>

Passing Arguments

HTMLRead-only
1
<button @click="greet('John')">Greet</button>

Event Object

HTMLRead-only
1
<button @click="handleClick($event)">Click</button>

Event Modifiers

ModifierDescription
.stopStops event propagation
.preventPrevents default action
.onceRuns only once
.captureUses capture mode
.selfTriggers only on element itself

Modifiers Example

HTMLRead-only
1
<form @submit.prevent="submitForm">
  <button type="submit">Submit</button>
</form>

Keyboard Events

HTMLRead-only
1
<input @keyup.enter="submit">

Best Practices

  • Use shorthand @ for cleaner code
  • Keep logic inside methods
  • Use modifiers to simplify event handling
  • Avoid inline complex logic

Common Mistakes

  • Writing complex logic inline
  • Not using event modifiers
  • Forgetting $event when needed
  • Mixing methods and inline logic incorrectly

Conclusion

The v-on directive is essential for handling user interactions in Vue. It provides a clean and efficient way to manage events.

Try it yourself

<button @click="count++">Click</button>
<p>{{ count }}</p>

Test Your Knowledge

Q1
of 3

v-on is used for?

A
Loop
B
Condition
C
Event
D
Binding
Q2
of 3

Shorthand of v-on?

A
#
B
@
C
:
D
$
Q3
of 3

Which prevents default action?

A
.stop
B
.prevent
C
.once
D
.self

Frequently Asked Questions

What is v-on?

It is used to handle DOM events in Vue.

What is shorthand for v-on?

The @ symbol.

What is .prevent?

It prevents default browser behavior.

Previous

vue v model

Next

vue custom directives

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.