I have a container component with a navbar component inside of it. I call an api in beforeCreate, then set variables here, and set them in vue 'state' (data), then pass those as props down to the navbar component. However, these do not appear. What am i doing wrong here?
<template>
<Navbar :title='title' />
<template>
<script>
export default{
name:'example',
data(){
return{
title:''
};
},
beforeCreate: function(){
axios.post('')
.then(response => {
this.title = response.title
})
}
}
</script>
I have console logged the response data from the api, so i know it is producing data properly. I have also console logged the data variable 'title' in the mounted lifecycle, and it has data. But the props for the navbar are still empty.
export default { }
inside your<script>
tag. Is it by mistake? - Sakshamaxios
request increated
method instead ofbeforecreate
- Josephthis.title = response.title
is guaranteed to be evaluated after the component is mounted. It's misleading to use beforeCreated for this but it's certainly not an issue here. - Estus Flask