简体   繁体   中英

vue props passing from parent component to child

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.

beforeCreate is called before data observation. Try again using beforeMount

Simply in the beforeCreate life cycle method your data object not loaded yet so you can't call it at this time so the best practice for this to use created method

beforeCreate()

This is the very first lifecycle hook that gets called in Vue JS, it gets called immediately after the Vue instance has been initialized.

created()

the second lifecycle hook that is called right after the beforeCreated hook. At this stage, the Vue instance has been initialized and has activated the start of things like computed properties, watchers, events, data properties and manipulations that come with it.

在此处输入图像描述

created: function(){
    axios.post('')
    .then(response => {
        this.title = response.title
   })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM