Passing arrays from child to parent components in VueJS: A comprehensive guide

I am currently utilizing the Vue-Multiselect plugin and attempting to send data up to a parent component so that a backend API can update certain records. However, I am facing challenges in achieving this successfully.

My approach (explained in detail) along with a CodeSandBox Demo can be viewed for a better understanding. Note: To view the functionality of the multiselect UI, navigate to POST-->ACME WIDGET and click "Edit" to interact with the multiselect plugin.

In my child component CustomerPOCSelect.vue, there are events such as onRemove and onSelect which push data into arrays named pocsToRemove and pocsToAdd respectively.

However, I need assistance in bridging the gap between this point and the parent component

EditPost.vue</code, where I can utilize the <code>onSubmitUpdate
event handler to actually update the server. Whenever a user makes changes to the multiselect input (additions or removals) and clicks the UPDATE POST button, I must pass on the emitted data to the onSubmitUpdate method.

EditPost.vue

<template>
  // Code for EditPost.vue template
</template>

<script>
// Script for EditPost.vue
</script>

CustomerPOCSelect.vue

<template>
  // Code for CustomerPOCSelect.vue template
</template>

<script>
// Script for CustomerPOCSelect.vue
</script>

Answer №1

To achieve this, you have the option of using either Vuex or leveraging custom events. Below is an illustration demonstrating how custom events can be utilized:

Parent Component

...
<ChildComponent @toParent="handler"/>

methods: {
  handler(value) {
    // Output Data Received from Child Component
    console.log(value) // "Dummy Data"
  },
}
...

ChildComponent

...
data() {
  return {
    myData: "Dummy Data"
  }
},
mounted() {
  // The emission of this event can occur in any method, not just in mounted
  this.$emit('toParent', this.myData)
},
...

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

There was a TypeError encountered in angular-highcharts, stating that it is not possible to read the property '0' of an undefined

Currently, I am utilizing the angular5 framework in conjunction with the angular-highcharts library to generate a basic map based on the highcharts demonstration available at https://www.highcharts.com/maps/demo/category-map. Here is a snippet of the code: ...

You should be providing a string value as expected. It seems that you may have overlooked exporting your component from the correct file it was defined in, or perhaps there is confusion with default and named

I encountered a strange error despite meticulously organizing and exporting/importing files. The code is structured from components to the App render method. Item.js import React from 'react'; import './Global.css' const Item = ({data ...

Guide on exporting data to PDF using Vuetify

I am looking to generate a PDF using Vuetify with image formatting. Here are the data points that I want to include in the PDF file: https://i.stack.imgur.com/gyh7x.png https://i.stack.imgur.com/tCUU8.png My challenge lies in creating tables, labels, an ...

Nuxtjs component fails to hot reload when v-if condition is modified

I am facing an issue with my navbar component which consists of two buttons, "Logout" and "Dashboard", that are visible only when the user is authenticated using v-if condition. The problem arises when the user clicks on the Logout button as the navbar do ...

Using Rails to fetch a remote page using AJAX

Currently using Rails3 and attempting to retrieve a remote page via ajax from (https://play.google.com/store/apps/details?id=). $.ajax({ url: app_url, type: 'GET', data: "id=<id>", crossDomain : true, dataType ...

Extracting the URL of the @font-face declaration in CSS with the use of JavaScript

Currently, my CSS file contains numerous @font-face tags. Each tag specifies a unique font-family and src URL. @font-face{ font-family: Garamond; src: url('ePrintFonts/pcl_91545.ttf'); } @font-face{ font-family: C ...

Is an audio player/playlist necessary for showcasing a mix engineer's skills in their

As a newcomer to the world of web development with some background knowledge from school, I work as a mix engineer and have created a portfolio website. Previously, I utilized Soundcloud and Spotify's API to showcase my mixes/songs, but the external J ...

Is there a way to add a scrollbar to the ChartJS tooltip to avoid data

I'm currently working on a chartJS project where I have a callback function to display data in tooltips. However, as the data increases, the overflow section of the tooltip gets hidden from the canvas. Is there a solution to prevent this overflow and ...

javascriptchange the format from <string, string[]> to <string, string>

In my code, I came across a JavaScript object that consists of key/value pairs. Each value in this object is an array of strings: var errors = { "Message": ["Error #1", "Error #2"], "Email": ["Error #3", "Error #4"] }; My goal is to transform thi ...

Encountering a surprise syntax error while trying to reference JavaScript from HTML?

After downloading this code from Github to make some modifications, I decided to run it locally. Link to the downloaded file The main index.html file references the JavaScript using: <script type="text/javascript" src="app.0ffbaba978f8a0c5e2d0.js">& ...

What is the technique for arranging the display of a component in React?

Is there a way to dynamically render a component in my react-app at a specific date and time, like 6.00PM on October 27, 2022? I want to release a form for signing up starting from that exact moment. The timestamp information will be stored in my database ...

What caused the failure of the ++ operator in production mode?

Encountering an issue with a reducer in my code. There is a button with an onClick function that triggers a NextBox action to alter the state of a React UseContext. The application was built using Vite, and while running npm run dev, everything functions a ...

How can we transform words with double "OO" in a paragraph into green squares using React? For instance, changing "clooney" to "cl[green square]ey"

import React, { Component } from 'react'; class App extends Component { constructor() { super(); this.state = { modifiedPar: "", newPar: "modified paragraph will be displayed here"}; } greenSquareMaker = word = ...

When combining Puppeteer with Electron, an error stating "Browser revision not found" may occur. However, this issue does not arise when running with Node

My attempts to make Puppeteer work in Electron have been with various versions of Puppeteer (v5.4.0, v5.4.1, and v5.5.0), on Windows 10/MacOS, and with different Node versions (v12, v14.0.1, v15.0.3). Trying a simple puppeteer.launch() in the main process ...

My jQuery Ajax seems to have a glitch, can someone help me figure out

Looking for some help with this HTML code: <section id="login"> <form> <input type="text" name="username" size="10" placeholder="username" /> <input type="password" name="password" size="10" placeholder="password" ...

Encountering a problem with a Vue component error stating "Unable to convert undefined or null to object"

Encountering an error message "Cannot convert undefined or null to object" with this VueJs Slider. You can see it in action on this website (It's the first component on the page). The slider's functionality is working fine, but I'm looking ...

Forever is causing email sending from the node server to fail

My API server is up and running with Node, featuring an add user function: add: function (userArray, onSuccess, onError) { userArray.forEach(function (user) { var length = 8, charset = "abcdefghijklnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345 ...

Tips for sending a div value that is not included in the model to the backend controller action

Question: I am looking to have a single div element in the user interface that can display a value added via JQuery and then be accessible in the backend once the form is submitted. View: @model ScheduleAptMV using (Html.BeginForm()) ...

When attempting to decrypt with a password using CryptoJS, AES decryption returns an empty result

Example The code snippet below is what I am currently using: <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script> <div id="decrypted">Please wait...</div> Insert new note:<input type="te ...

Simple methods to minimize the iteration of array loops in Node.js

First, retrieve the "grid_id" from the "grids" array ( Step 1 ) Next, verify if this "grid_id" is present in the "variationsDB" array ( Step 2 ) Once you have this "grid_id", use it to fetch the "var ...