【发布时间】:2018-11-08 15:36:51
【问题描述】:
知道我可能会有更多的孩子,有没有另一种方法可以在 VueJS 中做嵌套组件?
我事先不知道代码中会包含哪个组件,所以我使用动态组件,但它们中总会有多个子组件,而且数量并不总是相同
这是我找到的唯一解决方案 还有其他方法吗?
来自我的html:
<component :is="blocksElements[0].componentId">
<component :is="blocksElements[0].child.componentId" v-if="blocksElements[0].hasChild">
<component :is="blocksElements[0].child.child.componentId" v-if="blocksElements[0].child.hasChild" v-bind="blocksElements[0].child.child.props">
<component :is="blocksElements[0].child.child.child.componentId" v-if="blocksElements[0].child.child.hasChild" v-bind="blocksElements[0].child.child.child.props"></component>
</component>
</component>
</component>
来自我的 js:
blocksElements: [
{
componentId: 'row',
hasChild: true,
child: {
componentId: 'column',
hasChild: true,
child: {
componentId: 'block-image',
hasChild: true,
props: {
logo: true,
width: '120'
},
child: {
componentId: 'block-image',
hasChild: false,
props: {
logo: true,
width: '120'
}
}
}
}
}
]
【问题讨论】:
标签: javascript vue.js vuejs2 components nested-loops