【发布时间】:2017-12-11 12:22:00
【问题描述】:
我编写了一个组件(组件 B),它通过像这样的插槽接受自定义组件列表
// Component A
<div class="component-a">
...
<component-b>
<component-x></component-x>
<component-y></component-y>
</component-b>
...
</div>
我想将组件 x 和 y 包装在其他组件中,例如 li 标签。
// Output
...
<ul>
<li><component-x></component-x></li>
<li><component-y></component-y></li>
</ul>
...
我试过了
// Component B
<div class="component-b">
<ul>
<li v-for="item in $slots.default">
<component :is="item"></component>
</li>
</ul>
</div>
它不起作用。该项目是 VNode 对象,它不能用组件标签呈现。有什么办法可以解决这个问题吗?
编辑:我的包装组件不是li标签,它是我在组件B中设置的具有指定props的自定义组件。如果我从组件A包装它们,我需要重复编写自定义组件及其props。
Edit2: Render 函数也许可以解决这个问题,但我正在寻找 html 模板(单文件组件)的解决方案。
【问题讨论】:
-
也许这行得通
<component :is="item.name"></component>,只是一个想法。因为 :is 需要一个组件名称而不是对象引用。