Issue with Vue.js: Child component emitting event but parent component method not triggering

When a child component makes an input change, it emits an event with some data:

Vue.component('gum-option', {
    inheritAttrs: false,
    props: ['label'],
    template: `
        <label class="gum-option">
            <input 
                type="radio"
                v-bind="$attrs"
                v-on:change="$emit('update', this.label)">
            <span>{{ label }}</span>
        </label>
    `
});  

The parent component catches this event to trigger a method that displays an alert:

Vue.component('gum-select', {
    template: `
        <div v-on:update="updateValue"> 
            <label>{{label}}</label>
            <div class="selected">
                {{selectedOption}}
            </div>
            <slot v-bind="options"></slot>
        </div>
    `,
    data: function () {
        return {
            selectedOption: ''
        }
    },
    props:['options', 'name', 'label'],
    methods: {
        updateValue: function(event) { // method triggered by the "update" event
            alert(event); // Why isn't this working?
        }
    }
});


To see what's causing the issue, check out this pen: https://codepen.io/bourpie/pen/JjXdjzg?editors=1011

Answer №1

Ensure that the listener is placed on the gum-option component within the App file, along with the updateValue method. There were a few errors in the previous version, so here is the corrected demonstration:

<div id="myApp">
  <gum-select name="options" label="Options" v-bind:options="options" >
    <gum-option 
          v-for="option in options" 
          v-bind:key="option.value" 
          v-bind:value="option.value" 
          v-bind:label="option.texte" 
          name="province"
          v-on:update="updateValue">
    </gum-option>
  </gum-select>
</div>
Vue.component('gum-option', {
    inheritAttrs: false,
    props: ['label'],
    template: `
        <label class="gum-option">
            <input 
                type="radio"
                v-bind="$attrs"
                v-on:change="$emit('update', label)">
            <span>{{ label }}</span>
        </label>
    `
}); 
Vue.component('gum-select', {
    template: `
        <div> 
            <label>{{label}}</label>
            <div class="selected">
                {{selectedOption}}
            </div>
            <slot v-bind="options"></slot>
        </div>
    `,
    data: function () {
        return {
            selectedOption: ''
        }
    },
    props:['options', 'name', 'label'],
});
new Vue({
    el: '#myApp',
    data: { 
        options: [
            {value: '1', texte: 'Option 1'},
            {value: '2', texte: 'Option 2'},
            {value: '3', texte: 'Option 3'}
        ]
    },
  methods: {
        updateValue: function(label) { 
            console.log(label);
        }
    }
});

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

Attempting to eliminate the mouseleave event by utilizing the jQuery off function

I have a set of buttons in an array and I want to remove the event listeners for each button once it has been clicked. Here is my current approach: JavaScript: var currentButton; var selectedButtons = $("#moto-menu a"); function onEnter(){ TweenMax. ...

Achieving consistent scroll position when utilizing the history.js library to navigate back with the click of a button

I need help implementing a feature that allows users to return to the same position on a webpage when clicking the back button. A common example is on ecommerce sites where if you scroll down, click on a product, then go back, you should be taken back to t ...

Ways to showcase an item within additional items?

I'm struggling to properly display data in a table. My goal is to iterate through an object within another object inside an array and showcase the source, accountId, name, and sourceId in the table. https://i.sstatic.net/VVIuc.png <tbody clas ...

A standard procedure for evaluating an application using JavaScript on the client side

I am looking to streamline E2E testing for a web application. The frontend of the app is built on a JavaScript framework, while the backend uses Java technology. While I am aware of the tools and frameworks available for JavaScript testing, I am curious ...

JavaScript for controlling first-person movement with a mouse

Currently, I am working on implementing a first person movement feature using the mouse. While I have successfully implemented it using the keyboard, I am facing challenges with the mouse input. The issue arises from the ambiguity in movement directions ca ...

The compatibility between JSON and the CORS header 'Access-Control-Allow-Origin' is crucial

I am trying to obtain the value from a JSON file that is located on the server. To retrieve this value, I am using AJAX. However, when checking the console for errors, I receive the following message: Cross-Origin Request Blocked: The Same Origin Poli ...

Ways to stop Bootstrap 4 dropdown from appearing when clicking on an input field?

Here is a straightforward bootstrap dropdown menu example, but with a twist - the toggle element is a text input. Instead of showing the dropdown on click event, I want it to appear when the user inputs something so I can dynamically populate the menu base ...

Radio buttons with multiple levels

Looking to implement a unique two-level radio button feature for a specific option only. Currently, I have written a logic that will display additional radio buttons under the 'Spring' option. However, the issue is that when it's selected, t ...

Is the z-index feature not functioning as anticipated?

I'm currently working on a project involving a book that flips on click on both sides. The process involves checking the direction when a page is clicked and flipping it to the left if it's not to the right. Additionally, I adjust the z-index to ...

Error encountered: unable to locate module - '@deck.gl/exper-layers'

When attempting to run npm start on the trip example in deck.gl, I encountered an issue. Although other examples open without any problems, the trip example is giving me this npm error: Module not found: Error: Can't resolve '@deck.gl/experim ...

Personalized HTML selection list

When a select option is too long, it stretches the container to accommodate its length. I have created a truncate function to limit the display to 20 characters and add ellipses to prevent this stretching. I have been searching for a way to show the entir ...

"Djoser encounters a 400 Bad Request error when attempting to log in users

Currently working on an e-commerce application using django rest framework and vue. Utilizing djoser.authtoken for authentication purposes. Successfully managed to create a user with a POST request to /users/, but encountering issues while attempting to l ...

What could be the reason these two functions yield different outcomes?

I am currently in the process of optimizing a function to enhance performance. Previously, the old function took approximately 32 seconds, while the new one now only takes around 350 milliseconds for the same call. However, there seems to be an issue as th ...

What is the best way to set up an on-change listener for material-ui's <CustomInput...>?

I'm currently utilizing the React dashboard created by Creative Tim. I have a question regarding how to set up an onChange listener for a Here is the code snippet for the custom input class: import React from "react"; import classNames from "classna ...

What is the best way to display changing data in a React component?

Having some trouble with printing data based on the length of an array. Here is my attempted solution, but unfortunately, it's not working as expected. I believe the issue lies in the cb function within the forEach loop. Below is the code snippet: fun ...

Unable to accept the link ID in the modal

Presently, I am facing an issue with a modal that links a product with a customer and involves product discount calculations. The customer is automatically selected using the modal id, and the product is chosen through select options with a while loop. T ...

Having trouble sending JSON data to the server using a POST request

I am encountering an issue while attempting to send JSON data to the server using the fetch API and PHP as the server-side language. The PHP code on the server side is quite simple: <?php header("Access-Control-Allow-Origin: *"); header("Access ...

Transform the column's datetime into a date in the where condition when using sequelize

Hey there, I'm looking to convert a datetime column into just date format while running a query. Could someone assist me with creating a Sequelize query based on the example below? select * from ev_events where DATE(event_date) <= '2016-10- ...

During the testing of a Vue single file component with Jest, the prop being sent is found to be undefined

I am currently working on testing a Vue single file component that takes a prop as an input. However, I am encountering an issue when I try to mock the prop, which is an object. The problem arises in the HTML where the object values are utilized, resulting ...

Calculating the size of an array based on its attributes

Hey there, I'm working with an array containing attributes and need to determine its length. In this particular case, the array should have a length of 2 since there are only two "items" present. {"items":[{"value":"2","valor":0,"name":"Limpeza"}, {" ...