pwshub.com

How Event Handling Works in Vue 3: A Guide for Developers

How Event Handling Works in Vue 3: A Guide for Developers

Event handling in Vue 3 allows developers to respond to user interactions like clicks, key presses, form submissions, and more.

Vue provides simple and flexible ways to manage these interactions, enabling you to build dynamic and engaging applications.

In this guide, we'll cover:

  • Basic event handling (for example, click events)

  • Event modifiers like .prevent, .once, and .stop

  • Custom events between parent and child components

  • Handling events in forms

  • Keyboard events

  • The basics of emit

  • The basics of v-model

By the end, you'll be able to handle a wide range of events and improve user interaction in your Vue applications.

Basic Event Handling

Vue makes it easy to handle basic events like click, input, and submit directly in your template. You can use the @ symbol (short for v-on) to listen for events on DOM elements.

Example: Handling a Click Event

<template>
  <div>
    <button @click="handleClick">Click Me</button>
    <p>{{ message }}</p>
  </div>
</template>
<script setup>
import { ref } from 'vue';
const message = ref('Hello, Vue 3!');
function handleClick() {
  message.value = 'Button Clicked!';
}
</script>

Code explanation:

  • @click="handleClick": The @ symbol is shorthand for v-on. It listens for the click event and calls the handleClick method when the button is clicked.

  • message.value = 'Button Clicked!': In Vue 3's Composition API, ref creates reactive variables. When the button is clicked, the message reacts to variable updates, and the change is reflected in the DOM automatically.

This simple mechanism of listening to events and binding methods is foundational to handling user interactions in Vue.

Event Modifiers

Vue event modifiers allow you to control how events are handled, preventing default behavior or stopping propagation, for example. Common event modifiers include .prevent, .stop, .once, .capture, and .passive.

1. The .prevent Modifier

The .prevent modifier calls event.preventDefault(), preventing the default behavior of events like form submission.

Example: Using .prevent to Handle Form Submission
<template>
  <form @submit.prevent="handleSubmit">
    <input type="text" v-model="inputValue" />
    <button type="submit">Submit</button>
  </form>
  <p>{{ output }}</p>
</template>
<script setup>
import { ref } from 'vue';
const inputValue = ref('');
const output = ref('');
function handleSubmit() {
  output.value = `Form submitted with value: ${inputValue.value}`;
}
</script>

Code explanation:

  • @submit.prevent: Prevents the form from refreshing the page when submitted, allowing the handleSubmit function to process the form data instead.

  • v-model="inputValue": Two-way data binding between the form input and the inputValue reactive variable. It updates in real time as the user types.

When to use .prevent: Use .prevent when handling forms or other elements where you want to prevent the default behavior, such as preventing links from navigating.

2. The .stop Modifier

The .stop modifier calls event.stopPropagation(), preventing the event from bubbling up to parent elements.

Example: Prevent Event Bubbling
<template>
  <div @click="handleDivClick">
    <button @click.stop="handleButtonClick">Click Me</button>
  </div>
  <p>{{ message }}</p>
</template>
<script setup>
import { ref } from 'vue';
const message = ref('');
function handleDivClick() {
  message.value = 'Div clicked!';
}
function handleButtonClick() {
  message.value = 'Button clicked!';
}
</script>

Code explanation:

  • .stop: Clicking the button only triggers handleButtonClick and prevents the click from propagating to the parent div. Without .stop, clicking the button would also trigger handleDivClick.

When to use .stop: Use it to prevent parent elements from reacting to child element events.

3. The .once Modifier

The .once modifier ensures that the event listener is only called once.

Example: Handling a Click Event Once
<template>
  <button @click.once="handleClickOnce">Click Me Once</button>
  <p>{{ message }}</p>
</template>
<script setup>
import { ref } from 'vue';
const message = ref('');
function handleClickOnce() {
  message.value = 'Button clicked once!';
}
</script>

Code explanation:

  • .once: The handleClickOnce method is triggered the first time the button is clicked. Subsequent clicks do nothing because the event listener is removed after the first execution.

When to use .once: Use it for actions that should only happen once, such as a one-time form submission.

4. The .capture Modifier

The .capture modifier makes the event handler trigger during the capture phase rather than the bubbling phase.

Example: Handling an Event in the Capture Phase
<template>
  <div @click.capture="handleClickCapture">
    <button @click="handleClickButton">Click Me</button>
  </div>
  <p>{{ message }}</p>
</template>
<script setup>
import { ref } from 'vue';
const message = ref('');
function handleClickCapture() {
  message.value = 'Click event captured!';
}
function handleClickButton() {
  message.value = 'Button clicked!';
}
</script>

Code explanation:

  • .capture: The click on the parent div is handled first, before the child button’s click event, because the capture phase happens before the bubbling phase.

When to use .capture: Useful when you need to intercept an event before it reaches its target.

Custom Events

In Vue, child components can emit custom events to communicate with parent components. This pattern is commonly used to pass data or trigger methods in parent components.

Example: Emitting and Handling Custom Events

ParentComponent.vue:

<template>
  <ChildComponent @custom-event="handleCustomEvent" />
  <p>{{ parentMessage }}</p>
</template>
<script setup>
import { ref } from 'vue';
import ChildComponent from './ChildComponent.vue';
const parentMessage = ref('');
function handleCustomEvent(payload) {
  parentMessage.value = `Received custom event with payload: ${payload}`;
}
</script>

ChildComponent.vue:

<template>
  <button @click="emitCustomEvent">Emit Custom Event</button>
</template>
<script setup>
import { defineEmits } from 'vue';
const emit = defineEmits();
function emitCustomEvent() {
  emit('custom-event', 'Hello from ChildComponent');
}
</script>

Code explanation:

  • defineEmits(): This is used in the child component to define custom events. Here, the child emits a custom-event with a payload of 'Hello from ChildComponent'. (you can learn more details of emit from here)

  • Event Handling in Parent: The parent component listens for custom-event and responds by updating its parentMessage with the event payload.

When to use custom events: Use them for communication between parent and child components, especially for passing data from child to parent.

Event Handling in Forms

Vue’s v-model simplifies handling form inputs by creating two-way data binding between the form field and a data variable.

Example: Handling Input and Form Submission

<template>
  <form @submit.prevent="handleSubmit">
    <input v-model="formData.name" placeholder="Name" />
    <input v-model="formData.email" placeholder="Email" />
    <button type="submit">Submit</button>
  </form>
  <p>{{ formOutput }}</p>
</template>
<script setup>
import { ref } from 'vue';
const formData = ref({ name: '', email: '' });
const formOutput = ref('');
function handleSubmit() {
  formOutput.value = `Submitted Name: ${formData.value.name}, Email: ${formData.value.email}`;
}
</script>

Code explanation:

Keyboard Events

Vue also makes it easy to handle keyboard events such as keydown, keyup, and keypress.

Example: Handling Keyboard Events

<template>
  <input @keydown.enter="handleEnterKey" placeholder="Press Enter" />
  <p>{{ message }}</p>
</template>
<script setup>
import { ref } from 'vue';
const message = ref('');
function handleEnterKey() {
  message.value = 'Enter key pressed!';
}
</script>

Code explanation:

  • @keydown.enter: Listens for the enter keypress and triggers the handleEnterKey function when pressed. This is useful for form submissions or other actions that should be triggered by a specific key press.

Wrapping Up

Event handling in Vue 3 is pretty straightforward and flexible. From basic click events to custom events and form handling, Vue's event system allows you to create interactive, dynamic applications.

By using event modifiers and custom events, you can fine-tune how events are handled in your app. Understanding these techniques will enable you to create responsive, user-friendly interfaces.

Source: freecodecamp.org

Related stories
5 days ago - HELLO EVERYONE!!! It’s September 13th 2024 and you are reading the 28th edition of the Codeminer42’s tech news report. Let’s check out what the tech …
2 weeks ago - Let’s discuss Svelte's history and key features, why you might choose it for your next project, and what sets it apart from other frameworks. The post Svelte adoption guide: Overview, examples, and alternatives appeared first on LogRocket...
1 month ago - If you've built a frontend project in the last five years, you will have likely written some components, and maybe even used a component library. Components and libraries have been an important part of the web development landscape for...
3 weeks ago - MobX is a lightweight, boilerplate-free state management library that offers a reactive approach to state management. The post MobX adoption guide: Overview, examples, and alternatives appeared first on LogRocket Blog.
1 month ago - In this adoption guide, we’ll discuss some reasons to choose Fastify, key features, compare Fastify to some popular alternatives, and more. The post Fastify adoption guide: Overview, examples, and alternatives appeared first on LogRocket...
Other stories
2 hours ago - Ubuntu 24.10 ‘Oracular Oriole’ is released on October 13th, and as you’d expect from a new version of Ubuntu, it’s packed with new features. As a short-term release, Ubuntu 24.10 gets 9 months of ongoing updates, security patches, and...
3 hours ago - Did you know that CSS can play a significant role in web accessibility? While CSS primarily handles the visual presentation of a webpage, when you use it properly it can enhance the user’s experience and improve accessibility. In this...
5 hours ago - Design thinking workshops are your key to turning big problems into clear solutions. In this blog, I share how to run them efficiently and keep your team aligned. The post How to run a design thinking workshop appeared first on LogRocket...
5 hours ago - New memory-optimized X8g instances offer up to 3 TiB DDR5 memory, 192 vCPUs, and 50 Gbps network bandwidth, designed for memory-intensive workloads like databases, analytics, and caching with unparalleled price/performance and efficiency.
5 hours ago - Gain indispensable data engineering expertise through a hands-on specialization by DeepLearning.AI and AWS. This professional certificate covers ingestion, storage, querying, modeling, and more.