【发布时间】:2017-08-22 20:04:02
【问题描述】:
我正在使用嵌套组件,我想为该嵌套组件设置整个 data 属性,并使用父组件中的值。
比如第一个app.html组件
<NestedComponent data="{{ forNested }}" />
<script>
export default {
data: () => {
return { forNested: { a: 1, b, 2 }};
}
};
</script>
那么嵌套组件nested-component.html 将是:
<p>Hi {{ a }} and {{ b }}</p>
这意味着我必须这样做:
<p>Hi {{ data.a }} and {{ data.b }}</p>
是否有某种关键字属性可以做到这一点?
更新
由于没有展开运算符,我做了这样的事情,您可以在其中设置任意字段,例如data,然后观察它以更新所有属性。在嵌套组件中:
<div class="stuff">{{ someProperty }}</div>
<script>
export default {
oncreate: function() {
// Allow data to set all propeties, since we can't set
// all from the markup of a component
this.observe('data', d => {
if (_.isPlainObject(d)) {
this.set(d);
}
});
}
}
</script>
然后在使用嵌套组件时,forNestedObject 类似于 { someProperty: 123 }:
<NestedComponent data="{{ forNestedObject }}" />
【问题讨论】:
标签: javascript svelte