Javascript 如何传递对象';s属性作为vue.js组件的道具

Javascript 如何传递对象';s属性作为vue.js组件的道具,javascript,vue.js,vue-props,Javascript,Vue.js,Vue Props,鉴于以下组成部分: 导出默认值{ 名称:“MyComponent”, 道具:{ 蓝图:{ 类型:对象, 默认值(){ 返回{ 属性:0, 其他属性:5 } } } }, 数据(){ 返回{ 属性:this.blueprint.attribute, otherAttribute:this.blueprint.otherAttribute } } } 我想使用blueprintprop用一些默认值填充数据字段,这些默认值也可以在使用组件时定义 但是我怎么能只通过propblueprint的一个字

鉴于以下组成部分:


导出默认值{
名称:“MyComponent”,
道具:{
蓝图:{
类型:对象,
默认值(){
返回{
属性:0,
其他属性:5
}
}
}
},
数据(){
返回{
属性:this.blueprint.attribute,
otherAttribute:this.blueprint.otherAttribute
}
}
}
我想使用
blueprint
prop用一些默认值填充数据字段,这些默认值也可以在使用组件时定义

但是我怎么能只通过prop
blueprint
的一个字段呢? 当我这样做时:


当然,默认的
blueprint
otherAttribute
将消失

我可以只设置道具的一个字段,并将其与另一个字段的默认值合并,如下所示:



遗憾的是,
蓝图
道具有太多字段,无法单独传递每个字段。

我找到了一个似乎适合我的解决方案。我的组件现在如下所示:


导出默认值{
名称:“MyComponent”,
道具:{
蓝图:{
类型:对象
}
},
数据(){
返回{
属性:this.blueprint.attribute??0,
otherAttribute:this.blueprint.otherAttribute??5
}
}
}

我删除了道具的
default
部分,现在直接在数据中设置默认值。这样,如果我的
blueprint
prop不包含所有属性,其他默认值仍将存在。

是的,您的答案很好。这是我的解决办法

<script>
export default {
  name: 'MyComponent',
  props: {
    blueprint: {
      type: Object
    }
  },
  data () {
    return {
      blueprintDefault: {
          attribute: 0,
          otherAttribute: 5 
      }
    }
  },
  mounted () {
   this.blueprint = {...this.blueprintDefault, ...this.blueprint}
  }
}
</script>

导出默认值{
名称:“MyComponent”,
道具:{
蓝图:{
类型:对象
}
},
数据(){
返回{
蓝图默认值:{
属性:0,
其他属性:5
}
}
},
挂载(){
this.blueprint={…this.blueprintDefault,…this.blueprint}
}
}

MyComponent是由您构建的,还是来自其他库?它是由我构建的。我想我现在找到了解决办法,谢谢!