I recently began learning Vue.js 2, but I encountered an issue when passing props to a child component. Here's the code snippet where I pass the prop:
<div class="user">
<h3>{{ user.name }}</h3>
<depenses :user-id="user.id"></depenses>
</div>
Below is how I utilize it in my child component:
export default {
name: 'depenses',
props: ['userId'],
data () {
return {
depenses: []
}
},
mounted: function () {
this.getDepenses()
},
methods: {
getDepenses: function () {
this.$http.get('myurl' + this.userId).then(response => {
this.depenses = response.body
this.haveDepenses = true
}, response => {
console.log('Error with getDepenses')
})
}
}
}
The issue is that this.userId
is undefined in the JavaScript portion, even though I can display it using
<span>{{ userId }}</span>
. Additionally, the param userId with its value is visible in the Vue.js console.
Why am I getting an undefined value in the JS?