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
Shorthand Syntax
HTMLRead-only
1
Inline Event Handling
HTMLRead-only
1
Passing Arguments
HTMLRead-only
1
Event Object
HTMLRead-only
1
Event Modifiers
| Modifier | Description |
|---|---|
| .stop | Stops event propagation |
| .prevent | Prevents default action |
| .once | Runs only once |
| .capture | Uses capture mode |
| .self | Triggers only on element itself |
Modifiers Example
HTMLRead-only
1
Keyboard Events
HTMLRead-only
1
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.