简体   繁体   English

vue props 从父组件传递到子组件

[英]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.我在 beforeCreate 中调用了 api,然后在此处设置变量,并将它们设置为 vue 'state'(数据),然后将它们作为道具传递给导航栏组件。 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.我已经控制台记录了来自 api 的响应数据,所以我知道它正在正确生成数据。 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. beforeCreate在数据观察之前被调用。 Try again using beforeMount使用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生命周期方法中,您的data object 尚未加载,因此您此时无法调用它,因此使用created方法的最佳实践

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.这是在 Vue JS 中调用的第一个生命周期钩子,它在 Vue 实例初始化后立即调用。

created()创建()

the second lifecycle hook that is called right after the beforeCreated hook.beforeCreated钩子之后调用的第二个生命周期钩子。 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.在这个阶段,Vue 实例已经被初始化并激活了计算属性、观察者、事件、数据属性和随之而来的操作的开始。

在此处输入图像描述

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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