I am trying to pass data from a parent to a child component. However, the data I am trying to pass keeps printing out as blank in the child component. My code:
In
Please note the following:
Parent Component...
<template>
<div class="container">
<profile-form :user="user"></profile-form>
</div>
</template>
<script>
import ProfileForm from './ProfileForm'
Vue.component('profile-form', ProfileForm);
export default {
data: function () {
return {
user: ''
}
},
methods: {
getCurrentUser: function () {
auth.getCurrentUser(function(person) {
this.user = person
})
},
created: function() {
this.getCurrentUser();
},
}
</script>
Child Component...
<template>
<div class="container">
<h1>Profile Form Component</h1>
</div>
</template>
<script>
export default {
props: ['user'],
created: function () {
console.log('user data from parent component:')
console.log(this.user) //prints out an empty string
},
}
</script>
To pass data via props, you have to declare them in child component:
module.exports = {
props: ['user'],
created: function () {
console.log('user data from parent component:')
console.log(this.user) //prints out an empty string
}
}