This gist looks at the code needed to pass data from a child component to the parent component. It goes one step further and shows how to render the data in another child component.
$emitis used to send a custom event from the child to the parent componentv-on:submit.preventis attached to the form in the child component. Using.preventstops the browser from reloading when the form is submitted (default behaviour)methodsin both the child and parent component are used to handle sending and receiving form datapropsis used to pass data from the parent to child. In this example, props are being sent to both of the child components.v-forIs used to loop through the array entries
Used to organize and send information to the child components
In the template, add the two child components.
- Form Component
- use
v-onto connect the output of the child component function to the parent component function - use
v-bindto send the form fields object from the parent component to the child component
- use
<FormComponent v-on:formSubmitted="handleFormData" v-bind:dataGroup="formData" />
- Output Component
- use
v-bindto send the entries from the array in the parent to the output component to display it.
- use
<OutputComponent :outputEntries="formEntries">
- Naming conventions are very important for clarity.
- Create an object to send to the form component's props
formData: {
title: "",
description: ""
}
- Also create an empty array that will be used to take information from form.
- It will also be used to pass information to the other child component
formEntries: []
- This method is used to push each form entry to the empty array
methods: {
handleFormData(dataFromChild) {
this.formEntries.push(dataFromChild)
}
}
This component is used to take information from the user
- When putting a form into it's own component, you need to put a
v-on:submit.prevent="methodName"into the opening form tag. - See the
.FormComponent.vuefile for the an example
- In methods, you will create a function that will send the form submission content to the parent component
- Refer to this example:
methods: {
onSubmit() {
this.$emit("formSubmitted", {
propName: this.objectName.varName,
propName: this.objectName.varName
})
}
}
- This syntax is used to assign the values from the form input fields to object properties.
- replace
propNamewith the property title (ie: description) - replace
objectNamewith the prop name - replace
varNamewith
- replace
In this example, the parent component is used to direct the flow of information.
- add props for variables that will be passed from the parent component. Declare their type
props: {
dataGroup: {
type: Object
}
}
This component is used to display all the information from the forms submitted.
Add a v-for loop on one of the elements and create the layout for each entry.
<ul>
<li v-for="entry in outputEntries" :key="entry.id">
<h3>{{ entry.title }}</h3>
<p>{{ entry.description }}</p>
</li>
</ul>
Add a prop to accept the array from the parent component
props: {
outputEntries: {
type: Array
}
}