Skip to main content

Design & UX

Understanding Custom Events in Vue.js

Programmer Working With Program Code

Are You Struggling with Component Communication in Vue.js?

Building dynamic, maintainable applications in Vue.js requires seamless communication between components. But how can you ensure that your child components communicate effectively with their parent components—without creating a tangled mess of props, state management, and confusing logic?

Custom events in Vue.js offer a clean, simple, and powerful way to solve this problem, allowing your app to stay flexible, scalable, and easy to maintain. In this guide, we’ll walk through how to use custom events with Vue’s Options API to effortlessly pass data, trigger actions, and keep your components decoupled.

What Are Custom Events in Vue.js?

Custom events enable child components to send messages or data to their parent components. This is crucial when a child needs to:

  • Notify the parent about a change in state
  • Trigger actions or methods in the parent
  • Pass data back to the parent, such as user input or updates

Vue.js makes it simple to emit events from a child component and listen for those events in the parent, ensuring smooth communication between the two.

Why Should You Use Custom Events?

Custom events bring several key advantages to your Vue.js application:

  • Send Data to Parent Components: Easily emit data or updates from the child to the parent.
  • Trigger Parent Actions: Call parent methods or trigger actions from the child component.
  • Maintain Loose Coupling: Custom events allow you to keep components independent of each other, making your app easier to scale and maintain.

How to Emit Custom Events with this.$emit()

In Vue.js, the ‘this.$emit()’ method is used to trigger custom events from a child component to its parent. This allows the child to communicate with the parent, notifying it of actions like user interactions or state changes.

Syntax: this.$emit(‘event-name’, payload);

  • event-name: The name of the custom event to emit.
  • payload (optional): The data you want to pass with the event (could be a string, object, number, etc.).

Let’s walk through a practical example to see how custom events work.

Step-by-Step Guide to Emitting Custom Events in Vue.js

Example1: Emitting Custom Events with Arguments

Scenario: Task Management System

Child Component: (AddTask.vue)

Allows the user to add a new task to the list.

<template>
  <div>
    <input v-model="taskText" placeholder="Enter task" />
    <button @click="submitTask">Add Task</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      taskText: ''
    };
  },
  methods: {
    submitTask() {
      this.$emit('add-task', this.taskText);  // Emitting custom event with task text
    }
  }
}
</script>

Parent Component: (App.vue)

Manages a list of tasks and displays them.

<template>
  <div>
    <h1>Task List</h1>
    <AddTask @add-task="addTask" />
    <ul>
      <li v-for="task in tasks" :key="task.id">{{ task.id }} - {{ task.text }}</li>
    </ul>
  </div>
</template>

<script>
import AddTask from './AddTask.vue';

export default {
  components: {
    AddTask
  },
  data() {
    return {
      tasks: [],
      taskId: 1
    };
  },
  methods: {
    addTask(newTaskText) {
      this.tasks.push({
        id: this.taskId,
        text: newTaskText
      });
      this.taskId++;
    }
  }
}
</script>

Output:

Enter ‘Task One’ in the input box and click the ‘Add Task’ button to add the task.

Img 1

How it works:

In the Child Component (AddTask.vue):

  • We listen for the click event on the button and trigger the submitTask method.
  • Inside submitTask(), we use this.$emit(‘add-task’, this.taskText) to emit the custom event add-task with the entered task text as the data.

In the Parent Component (App.vue):

  • The parent listens for the add-task event using @add-task=”addTask”.
  • The addTask() method receives the task text as an argument, processes it, and adds it to the tasks array.

Example2: Emitting Custom Events with Object

Now, let’s look at a more complex example, where the child emits an object with user data, and the parent processes that data.

Scenario: Displaying User Information.

Child Component: (UserInfo.vue)

Contains user information and emits it as an object.

<template>
  <div>
    <button @click="sendUserData">Send User Info</button>
  </div>
</template>

<script>
export default {
  methods: {
    sendUserData() {
      const userData = { name: 'John Doe', age: 30 };
      this.$emit('user-data', userData);  // Emitting object with user data
    }
  }
}
</script>

Parent Component: (App.vue)

Listens for the user data and processes it.

<template>
  <div>
    <h1>User Information</h1>
    <UserInfo @user-data="handleUserData" />
    <p>Name: {{ userInfo.name }} </p>
    <p>Age{{ userInfo.age }}</p>
  </div>
</template>

<script>
import UserInfo from './UserInfo.vue';

export default {
  components: {
    UserInfo
  },
  data() {
    return {
      userInfo: {}
    };
  },
  methods: {
    handleUserData(userData) {
      this.userInfo = userData;  // Receiving and storing the user data
    }
  }
}
</script>

Output:

Click the button to view user information

Img 4

How It Works:

  1. In the Child Component (UserInfo.vue):
    • We emit an event with an object containing name and age as data.
  2. In the Parent Component (App.vue):
    • The parent listens for the event, receives the object, and logs or displays the user data.

Event Modifiers in Vue.js

Vue provides powerful event modifiers that give you greater control over custom events. For example:

  • .once: Fires the event listener only once.
  • .stop: Stops the event from propagating further.
  • .prevent: Prevents the default behavior of the event.
  • .capture: Listens during the capturing phase.

Here’s how to use them:

<ChildComponent @message-sent.once=”handleMessage” />

Conclusion: 

Custom events in Vue.js allow child components to communicate with their parents, making it easier to pass data, trigger actions, and maintain loose coupling between components. By understanding how to use this.$emit(), handle data passing, and utilize event modifiers, you can keep your Vue.js applications clean, maintainable, and scalable.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Anam Firdous

Anam Firdous, an associate technical consultant at Perficient, excels in front-end development, particularly in Sitecore, and holds expertise in SCORE. Her focus lies in crafting top-notch web solutions across diverse industries by leveraging various front-end technologies, showcasing her prowess in UI development.

More from this Author

Follow Us