给定以下组件:
<script>
export default {
name: 'MyComponent',
props: {
blueprint: {
type: Object,
default () {
return {
attribute: 0,
otherAttribute: 5
}
}
}
},
data () {
return {
attribute: this.blueprint.attribute,
otherAttribute: this.blueprint.otherAttribute
}
}
}
</script>
我想使用blueprint
prop 来使用一些默认值填充数据字段,这些默认值也可以在使用组件时定义。
但是我怎样才能只通过一个 prop 字段blueprint
呢?当我这样做时:
<my-component :blueprint="{attribute: someVar}" />
当然,otherAttribute
默认的blueprint
将消失。
我可以只设置 prop 的一个字段并将其与另一个字段的默认值合并,如下所示:
<my-component :blueprint.attribute="someVar" />
<!-- It doesn't work like this, but maybe you get the idea what I want -->
可悲的是,道blueprint
具有太多字段无法单独传递每个字段。