[Vuejs]-How to pass data to a child and render it in the child component?

0πŸ‘

βœ…

I can propose you that (and id need to be unique because it’s your key in the for loop)

parent:

<template>
  <div>
    <User :tileMenu="tileMenu" /> <!--pass all array-->
  </div>
</template>

<script>
import User from "./Home.vue";

const tileMenu = [
  { id: 1, name: "tile1NAME" },
  { id: 2, name: "tile2NAME" },
];

export default {
  components: {
    User,
  },
  data() {
    return {
      tileMenu: tileMenu,
    };
  },
};
</script>

child:

<template>
  <div>
    <div v-for="tile in tileMenuChild" :key="tile.id">
      {{ tile.name }} with id {{ tile.id }}
    </div>
  </div>
</template>

<script>
export default {
  props: {
    tileMenu: Array,
  },
  data() {
    return {
      tileMenuChild: this.tileMenu,
    };
  },
};
</script>

Leave a comment