Troubleshooting Vue.js $emit Event Triggering without Any Result

I am facing an issue where I am attempting to trigger an $emit event from a child component in Vue to execute a function in App.vue. Despite the event being detected by both the Vue and DOM inspectors, nothing happens upon triggering it.

I have gone through various troubleshooting steps such as changing the event name, confirming that functions work with a click event, binding to different components and divs, and even trying to emit the event without any payload. However, none of these approaches seem to resolve the issue. Testing on both Firefox and Chrome led to the same outcome: the event is called but no action occurs.

Here is the code snippet for the child component and method:

<div class="navbutton" v-on:click="clicked(data.id)">
  <p>{{data.text}}</p>
</div>
clicked(id){
switch(id){
    case 1:
       alert("One");
       break;
    case 2:
        this.$emit('testemit');
        break;
    case 3:
        alert("Three");
        break;
    case 4:
        alert("Four");
        break;
    default:
        alert("DEFAULT");
        break;
    }
}

And here is how the event is handled in App.vue:

<div v-on:testemit="EmitFunction"></div>
EmitFunction() {
    alert('MESSAGE');
}

My expectation is to see an alert displaying 'MESSAGE' when clicking on the second button. However, despite the other buttons (1, 3, & 4) functioning correctly, no alert is triggered as expected.

Answer №1

To make sure the emit is activated and listening on the parent component, follow these steps:

//app.vue
<template>
  <my-component v-on:testemit="EmitFunction"/>
</template>

Your 'EmitFunction()' in methods should perform the same actions as your 'clicked' function.

methods: {
  EmitFunction(id) {
    switch... etc

In your component...

//myComponent.vue
<div class="navbutton" v-on:click="$emit('testemit', id)">
  <p>{{data.text}}</p>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Generate seamless rotation within a specified time frame (+- x%)

I need to rotate a cube in my scene with a specific initial speed within a specified timeframe. The end angle of the cube must match the starting angle. To account for potential variations, I am allowing for a deviation of up to 5% in the timeframe. Cu ...

The issue of undefined return for multiple columns in MVC when using MCAutocomplete Jquery UI

My MVC multiple column is returning undefined. What am I missing or what is wrong with my code? Please help. https://i.sstatic.net/euwe8.png Controller public ActionResult EmployeeIDSearch(string term) { // Get Tags from data ...

"Master the art of using express and jade to loop through data and generate dynamic

Hey there! I have a question regarding node.js. When working with express, we typically have multiple files such as app.js, index.jade, index.js, and server.js where most of the server logic resides. Let's say we have two objects defined in server.js ...

Action creator incomplete when route changes

In my React-Redux application, there is an action creator that needs to make 4 server calls. The first three calls are asynchronous and the fourth call depends on the response of the third call. However, if a user changes the route before the response of t ...

Transfer sound data blob to server and store it as a file

Currently, I have 3 buttons on my webpage - one to start recording audio, one to stop the recording, and one to play the recorded audio. Using MediaRecorder makes it easy to capture audio as a blob data. However, I am facing issues when trying to upload th ...

Issue with child elements triggering dragenter and dragleave events in VUE 3

Working on incorporating dragenter and dragleave functionalities in VUE 3. However, the event is triggering within all child elements when it should only be executed by the parent element. To illustrate the issue, here's an example: https://jsfiddle. ...

Struggling to fetch the last and first name from a document in Firestore using VueJs

https://i.sstatic.net/IdZGi.pngWhen trying to retrieve the last name and first name from a document stored upon signup with a field displayName, I encountered an issue. Despite having a common key as displayName, which should allow me to fetch this informa ...

Guide on converting a JavaScript object into an array consisting of key and value pairs

I have information that appears as follows: [{"letter": "a", "word" : "apple"}, {"letter": "b", "word":"ball"}, {"letter": "a", "word":"alpha"}, {"letter": "c", "word":"cat"}] My goal is to convert it into: {"a":["apple", "alpha"], "b": ["ball"], "c":[" ...

Why is it impossible to nest schemas without using an array?

Can anyone explain why mongoose schema definitions don't allow something like this: var NameSchema = new mongoose.Schema({ first: {type: String, trim: true }, last: {type: String, trim: true } }); var UserSchema = new mongoose.Schema({ name: N ...

"Close button in pop-up window is malfunctioning due to excessive recursion causing a jQuery error

I have a button called button1 in my view. When this button is clicked, a popup is opened. Inside the popup, there is another button named button2. I want to close the first popup when button2 is clicked and open a different popup. Here is the code snippet ...

What is the best way to allocate mapState data in a list?

Currently, I'm tackling an apex bar chart project where I need to assign a list of mapState data into the apex series data. Here's the code snippet: <script> import { mapState } from 'vuex'; export default { data: () => ({ ...

Is it possible in CSS to ensure consistent width across various platforms and devices, regardless of their physical dimensions?

When it comes to altering the width (or any other properties) of an element using @media queries, there are multiple approaches available. One option is to utilize aspect pixel ratio, device width, and height. Find out more at https://developer.mozilla.or ...

Guide on implementing a date selector for each button/option clicked using Vue.js

My experience with Vuejs is still fresh, and I've incorporated 3 buttons named chart1, chart2, and chart3. Whenever any of these buttons are clicked, I want a Date selection to appear in a radio type format. You can see an example below: https://i.ss ...

I would like to implement a delay on this button so that when it is clicked, there is a pause before navigating

When I add the delay, it doesn't work, and when I remove it, it does. What can I do to make this button have a href link that delays before redirecting to another page? (I'm a newbie) I need to click and wait 3 seconds before navigating to other ...

Display various messages when submitting a form based on Ajax technology

I have been working on a script that utilizes AJAX to process form submissions. While I can successfully submit the form using AJAX and display a success message, I am looking to customize the messages based on the background data processing of the form. ...

Encountering a Laravel Vue API issue with an internal 500 server error indicating that the AppUser model

Encountering difficulties when attempting to post to phpMyAdmin while working locally. The site's routing is functioning properly overall. I have tried adding a model to the auth.php file and adjusting the namespace in user.php to include Models, as s ...

Overlay Image on Card, Overflowing into Card Body

I currently have a webpage featuring cards. Each card includes a thumbnail at the top with an overlay. The main content of the card needs to be positioned outside of the overlay, but it ends up overflowing onto it. Take a look at the demo: https://codepe ...

Uninterrupted text streaming with dynamic content that seamlessly transitions without any gaps

I'm currently facing a challenge with an outdated element like <marquee>. Here's a fiddle where you can check it out: https://jsfiddle.net/qbqz0kay/1/ This is just one of the many attempts I've made, and I'm struggling with two m ...

Guide to flattening a nested array within a parent array using lodash

Within my array named sports, there is another array called leagues, but I need to flatten these arrays using _.flatten due to issues with Angular filters. If you look at the data, inside the first array which contains a "name":"Live Betting", there is an ...

Replace the background image upon hovering over a div using jQuery

Creating Dynamic Backgrounds on Hover https://i.sstatic.net/K9wB2.jpg I am looking to implement a feature where users can hover over each red box in the picture, triggering a change in the background of the entire container (currently grey) to reveal a d ...