Vue 3 Props is not working on child components after binding it correctly. It returns undefined in child component

Tukei David

I am new to Vue and Laravel. This is my parent element, I am getting items from the database which is working fine. Then bind it to the child component.

<template>
  <div class="todoListContainer">
    <div class="heading">
      <h2 id="title">Todo List</h2>
      <AddItemForm></AddItemForm>
    </div>
    <ListView 
    v-bind:items="items"/>
  </div>
</template>

<script>

import AddItemForm from "./AddItemForm.vue"
import ListView from "./ListView.vue"

export default {
  components: {
    AddItemForm,
    ListView
  },
  mounted() {
    this.getList()
    console.log(this)
  },
  data() {
    return {
      items: [], // Stored all the items in an array and i binded it on ListView Component
    }
  },
  methods: {
    getList: function  () {
      axios.get('api/items')
      .then(response => {
        this.items = response.data
        console.log( this.items )
      })

      console.log( this.items )
    }
  }
}
</script>

In my Child components, when I try to console log the props it returns undefined. Below is my ListView child component.

<template>
  <div>
    <div 
    v-for="{item, index} in items" :key="index">
      <ListItem 
      v-bind:item="item" class="item"/>
    </div>
  </div>
</template>

<script>
  import ListItem from './ListItem.vue'
export default {
  // Props not working
  // On line 4 i looped through the items and then bind it to ListItem component
  props: [ 'items' ],
  components: {
    ListItem
  },
  created() {
    console.log( this.items )
  }

}
</script>

I have done everything by the book. Creating a method for collecting all the data and bind it to the child component.

Can someone help me on this?

hisablik

the child component is created before the api call is received, so not yet there in the created hook. Quick fix would be to add v-if so that the child component is rendered after items are ready:

   <ListView v-if="items.length > 0" v-bind:items="items"/>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Vue Props not working for child component

Can't correctly pass the props to the child component in Vue 3

Vue3 passing props to child component after async call

Vue 2 props on child component undefined

passing a callback function via props to child component is undefined in child vue

Vue Props in Child component

Undefined props in Vue js child component. But only in its script

Vue, child component says that props are undefined on view change

Vue binding parent and child components

Setting props of child component in vue

Vue - access props in child component

Vue child component wait for props

Vue - Access child component default props after mounted

How to create a method with props passed to child component (Vue3)

Passing props from parent component to child components

Vue parent component props are not passing to child component

Vue Prop undefined in child component

Vue 3 - Two-Way Data Binding Not Trouble With Child Component

why react props are passed undefined to the child component?

Destructed props sent to child component returning undefined

React child component receiving props as undefined

Pass props to child components after Async update

Passing state into child component as props not working

[ React.js ]Child component props not working

Accessing props in child vue component data function?

Vue props data not updating in child component

Passing props that will be used in style in child component - Vue

deleting child component removes that component and all child components created after it

Vue3 computed property in parent child component structure not working