In Vue, emitting does not trigger the parent component to update its properties

The emit is functioning properly, as I can observe in the vue developer tool. However, the property in the parent element is not updating.

Child Component:

<template>
  <div>
      <ul>
          <li v-for="(option, index) in options" :key="index" @click="selectOption(index)">
              {{ option }}
          </li>
      </ul>
  </div>
</template>

<script>
export default {
    name: "Dropdown",
    props: {
        options: {
            type: Array,
        },
    },
    data() {
        return {
            value: "",
        }
    },
    methods: {
        selectOption(id) {
            this.value = this.options[id];
            this.$emit("clickedOption", this.value);
        }
    }
}
</script>

Parent Component:

<button v-on:clickedOption="selectRole($event)">Select Role</button>

methods: {
        selectRole(value) {
            this.role = value.payload;
        },

It appears that the function selectRole is not being called.

Answer №1

To make the parent element work with the emit function, I had to code it this way:

<Dropdown :options="roleOptions" v-show="dropdownToggle" @clickedOption="selectRole($event)"></Dropdown>

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

Error encountered when WebDriverIO attempted to connect to Appium due to a lack of compatible capabilities

I'm currently facing an issue while attempting to link my virtual Android Device through Appium with my Webdriverio script for the purpose of automating some testing tasks. Here are the capabilities that I have set: capabilities: [{ // N ...

I'm wondering if there exists a method to arrange a multi-array in javascript, say with column 1 arranged in ascending order and column 2 arranged in descending order?

Here is an example of a randomly sorted multi-array: let arr = [[3, "C"], [2, "B"], [3, "Q"], [1, "A"], [2, "P"], [1, "O"]]; The goal is to sort it in the following order: arr = [[1, "O" ...

Tips for building a versatile fetch function that can be reused for various JSON formats within a React application

Using the fetch method in various components: fetch(url) .then(result => { if (!result.ok) { throw new Error("HTTP error " + result.status) } return result.json() }) .then(result => { ...

What is the best method for passing information to my frontend JavaScript files using Express?

When using ejs to render my html pages, I pass in data in the following manner: collection.find({}).toArray( function (err, results) { res.render('index', { results: results, ...

working with the express locals function

I've been trying to come up with a function that can access local variables, such as this one: // Retrieve user data by ID res.locals.findUser = function(user_id) { models.user.findOne({ '_id': user_id }, function(err, user) ...

Perform a series of observables from a dynamically generated array

Currently, I am in the midst of a project (Angular2) where I am dynamically creating Observables and storing them in an array. var ObservableArray : Observable<any>[] = []; //populating the Observable array dynamically for (var i = 0; i < this.ma ...

What are some ways to customize the appearance of the Material UI table header?

How can I customize the appearance of Material's UI table header? Perhaps by adding classes using useStyle. <TableHead > <TableRow > <TableCell hover>Dessert (100g serving)</TableCell> ...

In Vue 3, the old and new values returned by a deep watcher are consistently the same

const app = { data(){ return { form: { name: '', password: '' } } }, watch: { form: { handler(form, oldForm){ console.log(form, oldForm); }, deep: true } } ...

Using AJAX to handle 404 errors in Slim PHP

When I attempt to retrieve data using AJAX in my Slim PHP application, I am encountering a 404 (Not found) error in the console. The specific error message is as follows: http://localhost:8888/Project/mods/public/edit-mod/ajax/get-categories?gameID=1 404 ...

The functionality of the custom file upload button is experiencing issues on Microsoft Edge

I've been working on creating a unique custom image upload button that functions perfectly in Chrome, Firefox, and Opera based on my testing. However, I'm facing an issue where it doesn't work properly in Microsoft Edge. Feel free to check ...

How to efficiently pass props between components in NextJs

This is the project's file structure: components ├─homepage │ ├─index.jsx ├─location │ ├─index.jsx pages │ ├─location │ │ ├─[id].jsx │ ├─presentation │ │ ├─[id].jsx │ ├─_app.jsx │ ├─index.jsx ...

Real-time monitoring within a callback function in Angular 5

I need to ensure that a specific callback is executed only after receiving a response, starting from the line this.groupDefaultExpanded = -1; onwards. loadLoginDetails() { this.derivativeSpecService.getDerivativeDetails().subscribe( res => ...

Transform jQuery scroll script into vanilla JavaScript

While I am quite familiar with jQuery, I find myself struggling when it comes to using pure JavaScript. Could anyone provide guidance on how I can convert my jQuery code into vanilla JavaScript? Your help is greatly appreciated! Below is the code that I ...

What is the best way to assign the "active" class to a navigation list item using JavaScript within Bootstrap 4?

Update: just to clarify, I'm working on activating the navbar button based on the current page. I have a navigation bar set up and I am trying to dynamically add an "active" class to the li element when you are on that specific page. However, for som ...

JavaScript Bridge function in WebView not invoked

I encountered a strange issue with my application, which functions as an e-reader. To invoke functions for the web view, I utilize a JavaScript class: public class MyJavaScriptInterface { Context mContext; /* Instantiate the interface ...

When working with Vue JS, consider utilizing a data or computed property that is dependent on the value of a particular prop. In this case, the prop being altered is named "

While using pagination, I encountered an issue where the nextPage method works fine but the previousPage method throws an error. The error message warns against directly mutating a prop as it gets overwritten whenever the parent component re-renders. It s ...

Finding the latitude and longitude coordinates of a point at a specific distance from another using Node.js or JavaScript

My task involves determining the square area based on a latitude and longitude (x,y) as shown in the provided figure. https://i.sstatic.net/Rt7mC.png To accomplish this, I must calculate the coordinates of the remaining three corners by adding 10 kilomet ...

How do you trigger a function in a child component that was imported when a parent element is clicked?

Is it possible to access and call a function in an imported component from the "parent element"? I have multiple modules that I want to include dynamically in my app. I thought that if I import a component like Header in my main App.vue file, it would reco ...

Ways to ensure the first div always expands when sorting in AngularJS

In my AngularJS application, I have a set of div elements generated using ng-repeat. The first div is always expanded by default upon loading the page. Now, when I click a button to sort the divs based on their ID in the JSON data, I want the top div to ...

Step-by-step guide on eliminating the modal post-validation

Newbie in reactjs looking for help with modal validation issue. Goal: Press submit button inside modal, validate, then close the modal. Want to reuse modal for another row. Problem: I'm having trouble making the function work for a new row after ...