Pass multiple objects as props in Vue

featured Image

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:

  1. App.vue (Parent component), and
  2. 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.

In Parent Component:

<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.

In student component (Child component)

<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:

multiple object as props
Edit c40f5y

Related Posts

featured Image

How to add common header and footer in vuejs.

Here, in this article, we will learn how to add a common header and footer component in our vuejs application. In general, we always have the same header and footer…

Read more
featured Image

How to call a function in VUE template?

Here, we will learn on how to call a function in vue template. First, we have to define the function in the methods. and then we can use an event…

Read more
featured Image

How to redirect to external url in vue

Here, we will learn how to redirect to an external URL in vuejs application. Page Navigation is one of the basic features of a website. It helps us to navigate…

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *