Vue.js 使用v型,如果可用,仍然加载初始值

Vue.js 使用v型,如果可用,仍然加载初始值,vue.js,vuejs2,Vue.js,Vuejs2,我有一个子组件,我在其中传递customerData,如果可以作为道具使用的话。在该组件中,我将初始数据作为customer对象,并在其上添加字段。但是,如果customerData可用并且在对象中有该键,我希望将其显示为默认值。我怎么能在仍然使用v-model的情况下做到这一点 <child-component :customer-data="customerData" > </child-component> 儿童 data: {

我有一个子组件,我在其中传递
customerData
,如果可以作为道具使用的话。在该组件中,我将初始数据作为
customer
对象,并在其上添加字段。但是,如果customerData可用并且在对象中有该键,我希望将其显示为默认值。我怎么能在仍然使用v-model的情况下做到这一点

 <child-component  :customer-data="customerData" > </child-component>

儿童

data: {
     customer: {}
},
props: {
    customerData: {type: Object}
}

<div>
    <input v-model="customer.name" />
    <input v-model="customer.age" />
    <input v-model="customer.address" />
</div>
数据:{
客户:{}
},
道具:{
customerData:{type:Object}
}

您可以通过
道具中发送的值来更新
子组件的
客户
数据

const childcomponent=Vue.component('childcomponent'{
模板:“#子组件”,
数据:()=>({客户:{}}),
道具:{customer_data:{type:Object}},
创建(){
this.customer={…this.customer,…this.customer\u data};
}
});
新Vue({
el:“应用程序”,
数据:()=>({customer_data:{name:'name',address:'address'}}),
组件:{childcomponent}
});


因为在子组件中,您将道具定义为customerData,然后在父组件中,您需要传递相同的道具,即:customerData=“customerData”,那么您从父组件传递的数据和密钥将在子组件中可用。谢谢!将
装入()
而不是
创建()
有什么不利之处吗?另外,我能不能只做
this.customer=.this.customer\u data
?@HayleeGal58不客气。最好在
模板
准备好之前准备
数据
。关于第二点,我不这么认为,但你也可以用其他方式来做,这个想法是覆盖
customer
中每个属性的默认值,如果它在prop
customer\u data
中找到。如果你有其他问题,请告诉我,如果这个答案解决了你的问题,请接受它作为结束问题的解决方案。非常感谢…我以为
created()
是在收到道具之前运行的..我不知道组件在生命周期的那个阶段可以访问道具