Passing data from parent component to child component in vue.js

I am trying to pass data from parent to child component. However, the data I'm trying to pass remains empty in the child component. My code is:

B Profile.js(parent component)

<template>

    <div class="container">
        <profile-form :user ="user"></profile-form>
    </div>

</template>

<script>

import ProfileForm from './ProfileForm'

module.exports = {

    data: function () {
        return {
            user: ''
        }
    },

   methods: {

    getCurrentUser: function () {
        var self = this
        auth.getCurrentUser(function(person) {
            self.user = person
        })
    },

}

</script>

B ProfileForm.js(child component)

<template>

<div class="container">
    <h1>Profile Form Component</h1>
</div>  

</template>


<script>


module.exports = {


  created: function () {
    console.log('user data from parent component:')
    console.log(this.user) //prints out an empty string
  },


}

</script>

Note. Mine is userloading using my method getCurrentUser()... Can anyone help?

Thanks in advance!

+4
source share
1 answer

To pass data through the details, you must declare them in the child component :

module.exports = {   
  props: ['user'],

  created: function () {
    console.log('user data from parent component:')
    console.log(this.user) //prints out an empty string
  }
}
+11
source

Source: https://habr.com/ru/post/1652800/


All Articles