Skip to content Skip to sidebar Skip to footer

Vuejs - Pass Data From Parent To Child With Props

I have started playing with Vue but faced an issue when trying to pass data to a component using props. In the code below this.myData (in the Hello.vue) is undefined for some reaso

Solution 1:

You need to use the child component in the parent like, so:

// inside app.vue
<template><hello:myData='myData'></hello> // I believe this is your objective
</template> //:myData is binding the property from the components data field, without using it, you will get myData as `string` in the child component.

<script>exportdefault {
  name: 'app',
  data() {
    return {
      myData: 'this is my data'
    }
  },
  components: {
    Hello
  }
</script>

OP requested a way to pass in props to a child without rendering the child in the template.

So I am posting a link to the documentation

Vue.component('hello', {
  render: function (createElement) {
    returncreateElement(
      <tag-names>
    )
  },
  props: {
    myData: parentComponent.myData// you need to give the parent components' data here.
  }
})

Post a Comment for "Vuejs - Pass Data From Parent To Child With Props"