How to successfully send data props from child components to parent in Vue3

I am currently working on a personal project to add to my portfolio. The project involves creating a registration site where users can input their personal data, shipping information, and then review everything before submission.

To streamline the process, I have implemented a view called Register.vue that includes a Vuetify stepper with three steps: PersonalDataForm (a separate component), ShippingForm (another separate component), and a recap section.

One challenge I'm encountering is how to disable the "next" button on the stepper until the form is completed. Additionally, I am struggling with passing props to solve this issue.

In the REGISTER VIEW:

<template>
  <v-stepper color="deep-purple-darken-1" :items="['Personal Data', 'Shipping Address', 'Recap']" class="my-5">
    <template v-slot:item.1>
      <v-card>
        <PersonalDataForm @formDataSubmitted="handlePersonalDataForm" :isFormIncomplete="personalDataFormIsIncomplete"/>
      </v-card>
    </template>

    <template v-slot:item.2>
      <v-card>
        <ShippingForm @formDataSubmitted="handleShippingForm" :isFormIncomplete="shippingFormIsIncomplete"/>
      </v-card>
    </template>

    <template v-slot:item.3>
      <v-card>
        <Recap @completed="submitData" :disabled="isFormIncomplete"/>
      </v-card>
    </template>
  </v-stepper>
</template>

The PersonalDataForm.vue contains fields for first name, last name, birth date, birth place, and fiscal code. It utilizes Vuelidate for form validation.

The ShippingForm.vue includes fields for full name, region, province, city, house number, email, and phone number. Similar to the PersonalDataForm, it uses Vuelidate for validation.

Answer №1

If your data relationships are straightforward and linear, using the emit method is ideal. However, if you have complex nested structures, emit can be cumbersome as you need to navigate through various levels of hierarchy. This is where Pinia comes in handy. Pinia acts as a centralized storage system where you can define actions and access them globally. This simplifies your workflow significantly and removes the reliance on parent-child structures.

Answer №2

To simplify the process, consider using boolean flags for each type of form. When a form submission is valid, update the corresponding flag and enable/disable the next button accordingly.

One way to achieve this is by creating an event named formDataSubmitted and triggering this event upon successful form submission. Then, in the parent component, listen for this event and update the boolean flag accordingly.

<script setup>
const emit = defineEmits(['formDataSubmitted']);

function handleSubmit() {
  ...other code

  if(formValid) {
     emit('formDataSubmitted', formData);
  }
}
</script>

For more information on how events function in Vue, check out Vue Event Emitter.

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

ExpressJS subclasses are failing to inherit attributes and properties in callback functions

Currently, I am utilizing ExpressJS 4.16 on Node v8.9.4 and have established a base controller to manage default routes with the following example: class BaseController { constructor(name, app, router, data) { this._name = name; this._app = app; ...

AngularJS extension known as 'ngclipboard'

I've been attempting to utilize a plugin called ngclipboard in Angular, but something seems amiss as it's not functioning as expected. There are no error messages, however, the text from the input box is not being copied to the clipboard. To see ...

Why isn't my classList .add method working properly?

I've created a video slider background on my website, but I'm having trouble with the Javacript for video slider navigation. When I click on the button to change the video, nothing happens. The console is showing an error message that says "Canno ...

Tips for keeping the hover effect of a sibling element in Material-UI

Card Component import image from "../../Assets/pic.jpg"; import React, { useState } from "react"; import { makeStyles } from "@material-ui/core/styles"; import Card from "@material-ui/core/Card"; import CardActionAre ...

Exploring AngularJS and Jasmine: Testing a controller function that interacts with a service via $http

I encountered an issue while testing a controller that relies on a service. The problem arises because the service is currently set to null in order to focus solely on testing the controller. The current test setup is failing due to the BoardService being ...

Is there a way to remove a row through fetch using onclick in reactjs?

I'm completely new to this and struggling with deleting a row using fetch. I've written some messy code and have no idea if it will even work. Please help, I feel so lost... renderItem(data, index) { return <tr key={index} > &l ...

Identify unique special characters without the need for a specific key code

When you press the backspace key, the console may display an empty string for keyVal, which can be misleading because even though it appears empty, keyVal.length is actually equal to 1 due to a hidden character. element.on('keydown',function(e){ ...

npm package construction failed

https://i.stack.imgur.com/bk9r2.png Whenever I attempt to execute the command npm run watch, I consistently encounter this error message. Can someone please assist me with resolving this issue? Click on the image to view the problem. Thank you in advance. ...

JavaScript AJAX function is returning an undefined value rather than a boolean true or false

My AJAX call function in jQuery has a complete section with the following code: complete: function(XMLHttpRequest, textStatus) { if(textStatus == "success") { return(true); } else { return(false); } } However, when ...

What is causing the loss of data when attempting to access an array field within an object?

So I've been grappling with this issue. I have a collection of objects, each containing string arrays and a string based on the following interface: export interface Subscription { uid: string; books: Array<string>; } The problem arises whe ...

Leveraging dynamic identifiers within a string in a Vue.js application

I recently encountered an issue with a UIKit library I'm using for a tab component. The problem lies in the fact that it assigns the same ID to every tabbed component, making it difficult to uniquely identify them. While I appreciate the UI design, th ...

Compiling Typescript with module imports

In my project, I am working with two files named a.ts and b.ts. The interesting part is that file b exports something for file a to use. While the TypeScript compiler handles this setup perfectly, it fails to generate valid output for a browser environment ...

What can Cordova and express js bring to the table together?

I'm feeling pretty lost when it comes to displaying express views in a Cordova app client. It seems like the app would have to send a GET request and then the express application would render the view. But I'm unsure about how to actually make t ...

What is the process for choosing a specific id from a JSON structure?

Is there a way to extract specific data from a JSON format within an Ionic project? What is the process for selecting the ID associated with particular data in a JSON format? And how can I retrieve and display the value linked to the selected product&apos ...

Guide on creating a Jasmine test for a printer utility

Currently, I am working on writing a Jasmine test for the print function shown below: printContent( contentName: string ) { this._console.Information( `${this.codeName}.printContent: ${contentName}`) let printContents = document.getElementById( c ...

Avoid stopping Bootstrap Vue's events

Need help with a b-form-select control in Bootstrap Vue. Trying to execute a function when the value changes, but want the option to cancel the event and keep the original value. Complicating matters, the select is in a child component while the function ...

Step-by-step guide to dynamically generating v-models with VueJS

Recently, I've started delving into Vue and utilizing the latest build of VueJS 2. I have a dataset coming from the database that looks something like this: { count: 3, options: { 'red': 10, 'blue': 20, ...

The notify.js fails to display notifications in the event of an error

Why am I not receiving any error notifications even when there is an error message in the response object? $.ajax(settings).done(function (response) { if ( "error_message" in response ) { console.log(response); $.notify(" ...

Utilizing namespacing in a JavaScript library can enhance organization and flexibility, providing

Creating a JavaScript library with multiple modules is my next project. Imagine the library is named Library, with modules One and Two. My goal is to allow end users to call the library in two ways: Library.One.somefunction(params) or somefunction(param ...

Is there a way to use Ajax to analyze and contrast information from two separate web links?

I am looking to compare and display the genre_ids from the first link and the id from the second link. Example: genre_ids: [ 18, 878, ] { id: 18, name: "Drama", }, { id: 878, name: "Science Fiction", } Result: Drama, Science Fiction $(document).ready( ...