Pass multiple objects as props in Vue
Short tutorial on how to pass multiple objects as props to a child component from parent in vue using v-for directive.
In this short tutorial, we will see how to pass multiple objects as props from a parent component to a child component.
Let's take two components:
- App.vue (Parent component), and
- Student.vue (Child component)
Now we have to pass student objects from the parent component to the student component and display it to our template.
Let's see the code for each component.
<u>In Parent Component:</u>
<template> <div id="app"> <Student v-for="student in studentObj" :studentProp="student" :key="student.id" /> </div> </template> <script> import Student from "./components/Student"; export default { name: "App", data() { return { studentObj: [ { id: 1, name: "John" }, { id: 2, name: "Adam" }, ], }; }, components: { Student, }, }; </script>
Here, we have imported the Student component and used v-for directive to loop through the studentObj object in data.
<u>In student component (Child component)</u>
<template> <div class="student"> Student id :{{ studentProp.id }} , name: {{ studentProp.name }} </div> </template> <script> export default { name: "HelloWorld", props: { studentProp: Object, }, }; </script>
In the student component, we have declared the props as StudentProp, so that Vue knows what external props are passed to the component.
When the props are passed from the parent component, we just display them in our vue template.
Output:
Student id :1 , name: John Student id :2 , name: Adam
DEMO:

Related Posts
How to pass data from parent component to child using props in Vue
Find out how to send data from parent component to child component using props in Vue.
Execute a function from child component in Vue
Find out how we can execute a function in the parent component from the child component using emit function in Vue.
How to pass multiple props to component in Vue
Short article on how to pass multiple props to a component in vue using v-bind.
How to listen for props changes in VueJS?
This article is about how to listen for prop changes in vue. We will be vuejs watcher or watch property to listen for any changes in props.
