Troubleshooting Vue.js: Issues with emitting data from child to parent component

Currently, I am developing an application where users can view detailed information about cars in a sidebar upon clicking. I have encountered a few issues such as the sidebar appearing four times when it should only appear once. Although I managed to address this problem to some extent, I am still puzzled by why it keeps showing up multiple times. Moreover, I am facing difficulty receiving any response on the emit call. I have tried using $parent.$emit and $root.$emit, but unfortunately, these solutions do not seem to be working!!

<template>
  <div class="home">
    <!-- Displaying warehouse details -->
    <div
      v-for="(detail, detailindex) in details"
      :key="detailindex"
      class="container mt-5 mb-5"
    >
      <h1>
        {{ detail.name }}
        <span class="location">{{ detail.cars.location }}</span>
      </h1>
      <!-- Displaying vehicle details -->
      <SingleGarage :detail="detail"> </SingleGarage>
    </div>
      <b-sidebar
      id="my-sidebar"
      title="Sidebar with backdrop"
      backdrop-variant="dark"
      ref="mySidebar"
      backdrop
      shadow
      @emitData="testingEmit()"
    >
      <div class="px-3 py-2">
        <h1>{{currentCar}}</h1>
      </div>
    </b-sidebar>
  </div>
</template>

<script>
// @ represents /src folder
import axios from "axios";
import SingleGarage from "../components/SingleGarage";
export default {
  components: { SingleGarage },
  name: "Home",
  data: () => ({
    details: String,
    currentCar: 'String',
  }),
  methods:{
    testingEmit(data){
      this.currentCar = data
      console.log('data received from emit', data)
    }
  },
  mounted() {
    axios
      .get("https://api.jsonbin.io/b/5ebe673947a2266b1478d892")
      .then((response) => {
        var results;
        response.data.forEach((element) => {
          element.cars.vehicles.sort((a, b) => {
            a = new Date(a.date_added);
            b = new Date(b.date_added);
            results = a > b ? -1 : a < b ? 1 : 0;
            return results * -1;
          });
         });
        this.details = response.data;
       });
  },
};
</script>

... (remaining text remains unchanged for uniqueness)

Thank you for your assistance.

Answer №1

If you're encountering an issue, here are a few solutions to try:

In your Home.vue file, make the following adjustment:

@emitData="testingEmit"
// or
@emitData="testingEmit($event)"

By removing the parentheses after testingEmit, Vue will correctly handle any data passed from the $event. Alternatively, you can add $event as a parameter in testingEmit.

Next, in your SingleGarage.vue file, update $parent.$emit with:

this.$emit('emitData',this.detail.cars.vehicles[vehicleIndex].make)

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

What is the best method to display the content in the second response for ajax's authorization and dealing with cors?

I have successfully implemented basic authorization and enabled CORS on my VPS. Check the CORS preflight request using cURL: HTTP/1.1 200 OK Date: Sat, 15 Sep 2018 08:07:37 GMT Server: Apache/2.4.6 (CentOS) Access-Control-Allow-Origin: http://127.0.0 ...

Assigning the variable "name" attribute to an <input> element in HTML: A step-by-step guide

In the image below, I am working on creating a user interface using JSP and loops. I am looking to assign unique names to each input element to differentiate them. To achieve this, I plan to give them variable name attributes which I can set and populate ...

Utilizing the Express-busboy npm package to generate a new directory within the public folder of

While working on my controller, I encountered an issue when trying to readFile sent from the browser via AJAX. Unexpectedly, a directory was created in my public folder with a name like '3d6c3049-839b-40ce-9aa3-b76f08bf140b' -> file -> ...

The 'replace' property is not found in the 'IData' type. What is the missing piece?

I am trying to implement a cast function that can handle strings containing numbers as input. Unfortunately, I am encountering an issue with the "replace()" method when using regex. Does anyone know how I can define the use of "replace()" within the IData ...

How can the event listener be utilized with md-autocomplete?

I am currently in the process of developing an angularjs application that incorporates an autocomplete feature. One key aspect of this application is that certain fields cannot be populated until an item has been selected using the autocomplete function. ...

Error in Node: Module 'write-file-atomic' not found

https://i.sstatic.net/CKPjL.png When attempting to install packages using NPM, I keep encountering the same error message. This issue arises consistently for any operation such as "init," "start," or "install." ...

Angular SPA boasting an impressive one million pages

Currently, I am in the process of transitioning my webshop, which contains numerous products, to an Angular SPA application using ngRoute and HTML5 mode. I have come up with a method where I receive some of my routes from the server as a JSON object, but s ...

How can you identify if JavaScript is turned off without relying on the noscript tag?

While working on designing a pop-up notification for cases when JavaScript is disabled, I incorporated html, css, and bootstrap elements into my project. An issue arose as I pondered if there exists a method to identify whether the html class within my p ...

Just updated to Angular 10, encountered issue: Unable to modify the read-only property 'listName' of an object

After updating my Angular project from version 8 to version 10, I encountered an error while trying to edit an input field in a Material Dialog. The error message displayed is as follows: ERROR TypeError: Cannot assign to read only property 'listName& ...

Combining two elements in Angular 2

I am looking to find the intersection of two objects. My goal is to compare these objects and if they have matching values on corresponding keys, then I want to add them to a new object. obj1 = { "Projects": [ "test" ], "Companies": [ "facebook", "google ...

Creating a delete functionality button in JavaScript is essential for removing elements from a webpage with ease. This button allows users to seamlessly

I am currently working on an HTML form and here is the code for it: <div id="format"> <form id="myForm" onsubmit="myForm(event)"> <b>Name:</b></br> <input type="text" name="name" id="name" r ...

Issue TS7053 occurs when trying to access any index of the target of a React.FormEvent<HTMLFormElement>

I've been working on adapting this tutorial to React and TypeScript. Here is the code snippet I have implemented for handling the onSubmit event: const handleSignUp = (event: React.FormEvent<HTMLFormElement>) => { event.preventDefault(); ...

In JavaScript ES6, the const keyword can unexpectedly be altered even though it is intended to be constant

While experimenting with ES6, I came across an interesting feature - const values cannot be changed or reassigned. However, I noticed that in certain scenarios, it seems like we can change them. Can someone explain this? { const name = 'unchangabl ...

execute node.js scripts through a gulp task

I'm looking for a way to run multiple JavaScript scripts located in the same folder (scripts/*.js) using a gulp task, instead of having to manually execute each script with 'node script.js' individually. Can anyone help me achieve this? May ...

The error message "Royalslider - The property 'ev' is not defined on 'slider.ev.on'" appeared on the screen

Discussing the topic: Adding input to active slide I'm still struggling with this issue, but I believe I'm very close. I'm stuck at the moment. The code seems correct..but I keep encountering errors. Particularly with "slider.ev.on" - Uncau ...

What would be considered the optimum folder layout for organizing parent and child components within an Angular project?

https://i.sstatic.net/0mLJh.png Displayed above is the structure of parent and child components being implemented. While I have a grasp on Angular's basic folder structure, I am in need of some clarifications. Below you will find the current structur ...

It is possible to retrieve a variable from a secondary table within an API on a list page, but it is not possible to do

The functionality of this page, which reads and displays a full table from an API, is working flawlessly; <template> <b-col> <h2> Enrolments <b-button :to="{ name: 'createEnrolment&apos ...

Using a single component more than once within react-router configuration

Within my React application, I have a class named MainContainer that is responsible for displaying content. This MainContainer class has a child component called SidebarContainer which lists various links. By default, when rendering the main container, the ...

Executing code before a component mounts in ReactJs: What to do now that componentWillMount is no longer recommended?

Before my component mounts, I want to redirect the user to the home page based on a condition: componentWillMount(){ console.log("componentWillMount is called.") let userHasNotChosenOpportunity = true if (userHasNotChosenOpportunity) ...

What is the process for creating URL query parameters?

What is the process for generating parameters after node?_=xxxxxx? If I am using a Python script to access the URL, how can I retrieve these parameters? edit: I apologize for not providing enough information. This is my first post as a novice. I am atte ...