Is there a way to retrieve JSON data from a child component and pass it to a parent component in Vue.js?

I have data from the results stored in a child component that needs to be passed to the main component.

The Main Component is the parent, so whenever I click the button, the results should be collected in the main app

<button @click="showFinalResult">Click me</button>

The data is currently in the Child component and I just need to display the results in JSON format in the Parent. The results are input results.

results: [],

Answer №1

If you want to send data from a child component, you can emit an event:

Vue.component('Child', {
  template: `
    <div class="">
      
    </div>
  `,
  data() {
    return {
      results: [1,2,3]
    }
  },
  mounted() {
    this.$emit('update', this.results);
  }
})

new Vue({
  el: '#demo',
  data() {
    return {
      res: [],
      show: false
    }
  },
  methods: {
    getResult(res) {
      this.res = res
    },
    showResults() {
      this.show = !this.show
    }
  }
})

Vue.config.productionTip = false
Vue.config.devtools = false
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <button @click="showResults">Click me</button>
  <p v-if="show">{{ res }}</p>
  <child @update="getResult" />
</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

Issues with routing in Laravel and Vue.js application causing unexpected behavior

Having issues with Laravel + Vue.js application routing. When I access my Laravel + Vue.js application at "", everything works fine. However, when I run the application on XAMPP server using "http://localhost/Ecommerce/", the vue-router stops working. Fo ...

Turn off email alerts for items and folders in ALFRESCO 5.2

Here's a snippet of JS code I created to toggle notifications with a button click: (Action.min.js): var me = this, jsNode = record.jsNode, content = jsNode.isContainer ? "folder" : "document"; if (jsNode.hasAspect("cm:emailed") ...

Send a bundle of data through AJAX requests

An issue has been encountered on an HTML/PHP page named sucessful.php where a variable job_id passed from another page is not being received by the destination page interview.php. The problem arises when attempting to transfer two variables and their corr ...

Retrieve the chosen element from a jstree

How can I retrieve the selected Node from a jstree? This snippet shows the code in the View section: <div id="divtree" > <ul id="tree" > @foreach (var m in Model.presidentList) { <li class="jstree-clicked ...

Executing two distinct SQL queries within one nodejs function

I'm facing an issue with updating two tables in my database - the stockmaster table and the prodstock table. I've been trying to run a query using a function to update both tables simultaneously, but unfortunately, it's not working as expect ...

Tips for creating a scrolling animation effect with images

I'm currently attempting to create an animation on an image using HTML/CSS. The issue I'm facing is that the animation triggers upon page load, but I would like it to activate when scrolling down to the image. Below is my snippet of HTML: <fi ...

Insert an array inside another array using JavaScript (jQuery)

I've been attempting to use the push() method within a loop to construct a data structure as shown below: var locations2 = [ ['User', position.coords.latitude, position.coords.longitude, 1], ['Bondi Beach', -33.890542, 151 ...

TestCafe has encountered an issue: "There are no tests available to run. This may be due to either the test files not containing any tests or the filter function being too

Attempting to run automated tests using TestCafe resulted in an error when executing the following command. testcafe chrome testc.ts The specified command was used to test the testc.ts file within my Angular application, with TestCafe installed globally ...

Retrieve all Tableau workbooks stored on the server

I am currently working with Tableau Server and have multiple workbooks published on it. My goal is to create a dropdown list that displays all the workbook names along with their corresponding URLs. This way, when a user selects a value from the dropdown, ...

Is it possible to use NextJS to simultaneously build multiple projects and export them as static sites?

I'm currently working on a small Next.js project where I retrieve data from various API endpoints. These endpoints typically follow this format: https://enpoint.com/some-query/project1 The interesting thing about the API is that it can return differe ...

Why isn't my Vue2 data updating in the HTML?

Starting my journey with Vue, I have been finding my way through the documentation and seeking support from the Vue community on Stack Overflow. Slowly but steadily, I am gaining a better understanding of how to create more complex components. The issue I ...

Understanding the significance of underscores in JavaScript strings

Some places mention using _() around strings like _('some string'). For instance, in a desktop program with these imports: const Applet = imports.ui.applet; const St = imports.gi.St; const Gdk = imports.gi.Gdk; const Gtk = imports.gi.Gtk; const ...

Navigating the loading of information with fetch and React Hooks

As a newcomer to React and still learning JavaScript, I am facing a challenge with handling useEffect alongside an asynchronous function. My goal is to fetch data from an API and render a quote in the paragraph with id of text, along with the author's ...

Leveraging jquery's setInterval for automating tasks like a cronjob

I've been experimenting with Cronjobs and I've run into a roadblock. My goal is to have the cronjob execute every X minutes, containing a script with JavaScript that calls an ajax request every second for the next 60 seconds. The ajax call trigge ...

Tips for smoothly switching from one SVG image to another

I am looking for a way to smoothly transition between two SVG images that are set as background images in a div. I am specifically interested in using CSS 3 transitions for this effect, but I am open to other solutions as well. Can you help me achieve th ...

Converting a multipart form data string into JSON format

Can you help me figure out how to convert a multipart form data into a JSON object in Node.js? I've been looking for the right module but haven't had any luck so far. Here is an example of my form data: ------WebKitFormBoundaryZfql9GlVvi0vwMml& ...

Switch back and forth between two tabs positioned vertically on a webpage without affecting any other elements of the page

I've been tasked with creating two toggle tabs/buttons in a single column on a website where visitors can switch between them without affecting the page's other elements. The goal is to emulate the style of the Personal and Business tabs found on ...

Advice for transferring a Java variable to another JSP page with embedded JavaScript code

This is a snippet of my Java class: @RequestMapping(value = "/front", method = RequestMethod.GET) public String oneMethod(@RequestParam String name, Model model) { String str = "something"; model.addAttribute("str", str); return "jsppage"; } ...

Tips for sending a command to the server using the ssh2-promise package in Node.js

In my current code, I am utilizing an SSH library to establish a connection to a server, create a shell session, and send a password to authenticate. However, I am encountering an issue where the password is not being sent as intended. Upon some debugging ...

Interactive form featuring text fields and dropdown menus

Is it possible to create a form with the ability to add new rows consisting of SELECT and INPUT elements? Although my current code allows for this, I am facing an issue where no variable from the SELECT element is posted when the form is submitted. This i ...