How to pass data from parent to child component in Vue 3 (Options API and Composition API)
In Vue 3, we use Props or defineProps to pass data from a parent component to a child component. Props are the cusotm attributes. By using props, we can send the data from the parent to a child component. If you need to pass data from a child component to a parent component, you need to use emit.
The following example project includes two components; one is called ParentComponent and the other is called ChildComponent. In this tutorial, I will show you how to send name and title variables to the child component.
Prerequisites:
The reader needs to have basic knowledge about Vue JS and Components
In this tutorial, I'll show you how to use props in Vue 3 utilising the
- Options API
- Composition API Setup()
- Composition API <Script Setup>
How to pass data from parent to child component in the Vue 3 using Options API
ParentComponent:
- I added a name and greeting property to the parent component's data property.
- Before providing the props to a specific component, we must first import that component.
- I'm imported a child component utilising the import statement.
- In the components object, specify the component name.
- I defined the name and greeting properties in data().
- We can send props to specific component tags in that template tag.
- I'm sending name and greeting props using the childcomponent tag.
ChildComponent:
- You must define receiving property names in the props array in the child component. I defined greetingtochild and nametochild in the props property, and we can now use them in the template section.
- If you wish to use those props values within the script part, use this keyword, for example: this.propsName
How to pass data from parent to child component in Vue 3 using the Composition API setup()
ParentComponent:
- If the props are dynamic ones, we must define them in the setup method of the parent component before importing the child component.
- I'm using the ref() function to make our values reactive in this case.
- In the template part of the vue file, we must return the values we need to use.
- We are able to pass props in that template tag.
- I'm sending the salutation and name using the child component tag.
ChildComponent:
- You must specify the names of the receiving property variables inside the props array. I did so by defining the properties greetingtochild and nametochild inside the props array, and we can now utilize them in the template tag.
- You must send the props parameters to the setup method and use the value, for example, props.propertyName; if you wish to use those props values inside the script Part.
Note: If strongly typed properties are required, properties can be defined in the object
with their data type.
How to pass data from parent to child component in Vue 3 using the Composition API <script setup>
ParentComponent:
- I imported a child component using the import statement.
- Since I'm utilizing the ref() function to make our values reactive, you don't need to explicitly describe the component names that are imported into this component while script setup.
- The values that were declared during script setup do not need to be returned.
- We are able to pass props in that template tag.
- I'm sending greeting and name props using the child component tag.
ChildComponent:
- In child, you need to declare props values inside the defineProps macro
- You can define those props with value types and without value types as well.
- Now we can use them in the template part.
- If you want to use those props values inside the script part, example: props.propsName
Tags:
Vue Js