Vue.js 3.0 Tutorial
Vue.js 3.0 Component Advanced
Vue.js 3.0 Transitions & Animations
Vue.js 3.0 Reusable & Combinable
Vue.js 3.0 Advanced
Vue.js 3.0 Tools
Vue.js 3.0 Scale
Vue.js 3.0 Accessibility
Vue.js 3.0 Migrating from Vue2
Vue.js 3.0 Contribute Documentation
Vue.js 3.0 API References
Vue.js 3.0 Style Guide
Handling events in Vue 3.0 is quite straightforward and similar to Vue 2. Here is a simple tutorial showing how you can handle a button click event.
Step 1: Initialize your Vue project.
Create a new Vue 3 project using Vue CLI.
npm install -g @vue/cli vue create my-project
Choose Vue 3 when asked which version of Vue to use.
Step 2: In your App.vue
file (or any other component where you want to handle events), create a method to handle the click event and bind it to the button element using the v-on
directive (or its shorthand @
).
Here's an example:
<template> <div> <h1>Event Handling in Vue 3</h1> <button @click="handleClick">Click me!</button> </div> </template> <script> export default { methods: { handleClick() { alert('Button was clicked!'); }, }, }; </script>
In this example, when the button is clicked, an alert will pop up with the message "Button was clicked!".
Step 3: Run your application.
You can run your Vue 3 application with the following command:
npm run serve
Now, when you load the app in your browser and click the button, you should see an alert saying "Button was clicked!".
This is a basic example. Vue's event handling is capable of much more, including event modifiers (like .prevent
and .stop
), key modifiers for keyboard events, and handling custom events with $emit
.
If you want to handle the event in the setup()
function using the Composition API, you can do so by defining a function and returning it like this:
<template> <div> <h1>Event Handling in Vue 3</h1> <button @click="handleClick">Click me!</button> </div> </template> <script> import { ref } from 'vue'; export default { setup() { const handleClick = () => { alert('Button was clicked!'); }; return { handleClick }; }, }; </script>
Here, the handleClick
function is created within the setup
function and then returned so it can be used within the template.
v-on Directive:
The v-on
directive is used for event handling in Vue 3.0.
It allows you to listen to DOM events and execute some code in response.
Example code:
<button v-on:click="handleClick">Click me</button>
methods: { handleClick() { console.log('Button clicked!'); } }
Event Modifiers:
Event modifiers are used to modify the behavior of event handlers.
Example code:
<!-- Stop event propagation --> <a v-on:click.stop="handleClick">Click me</a> <!-- Prevent the default behavior --> <form v-on:submit.prevent="handleSubmit">Submit</form>
Event Binding:
You can bind events dynamically using the v-bind
directive.
Example code:
<button v-bind:click="dynamicEvent">Click me</button>
data() { return { dynamicEvent: 'handleClick' }; }
Custom Event Handling:
Vue components can emit custom events using this.$emit
.
Example code:
<custom-component v-on:custom-event="handleCustomEvent"></custom-component>
methods: { handleCustomEvent(payload) { console.log('Custom event handled with payload:', payload); } }
Event Delegation:
Vue supports event delegation through the use of event modifiers.
Example code:
<ul v-on:click.self="handleClick"> <li>Item 1</li> <li>Item 2</li> </ul>
Event Listeners:
You can attach event listeners directly to elements.
Example code:
<div v-on="{ click: handleClick, mouseover: handleMouseOver }">Hover me</div>
Event Propagation:
Events in Vue follow the DOM event propagation model.
You can stop propagation using stopPropagation
.
Example code:
<div v-on:click="handleClick"> <button v-on:click.stop="handleButtonClick">Click me</button> </div>
Native Event Handling:
Use the .native
modifier to listen to native DOM events.
Example code:
<custom-component v-on:click.native="handleNativeClick"></custom-component>
Emitting Custom Events:
Components can emit custom events using this.$emit
.
Example code:
this.$emit('custom-event', eventData);
Handling Keyboard Events:
Listen for keyboard events like keydown
, keyup
, etc.
Example code:
<input v-on:keyup.enter="handleEnterKey">
Handling Mouse Events:
Capture mouse events like click
, mouseover
, etc.
Example code:
<div v-on:mouseenter="handleMouseEnter">Hover me</div>
Handling Form Events:
Manage form-related events such as submit
, input
, etc.
Example code:
<form v-on:submit="handleSubmit"> <input v-on:input="handleInput" /> <button type="submit">Submit</button> </form>