【问题标题】:Can you render VNodes in a Vue template?你可以在 Vue 模板中渲染 VNode 吗?
【发布时间】:2024-07-08 12:25:01
【问题描述】:

我有一个渲染函数将一些数据传递到一个作用域插槽的情况。作为此数据的一部分,我想包括一些由渲染函数构造的 VNode,它们可以选择由作用域插槽使用。在模板中编写作用域插槽以输出接收到的原始 VNode 时是否有任何问题?

【问题讨论】:

  • 是的,这是一个有趣的想法,听起来像是要做的事情。该示例程序中的哪个组件做到了这一点?我明白了,TodoList、BaseInputText 和 TodoListItem。
  • 伙计,我搞砸了; this is the correct link。它是第二个示例中的 vnode 组件。
  • 我喜欢。我怀疑这对我有用。谢谢分享。

标签: vue.js vuejs2 vue-component


【解决方案1】:

您可以使用功能组件为模板的该部分呈现 vnode:

<some-component>
  <div slot-scope="{ vnodes }">
    <vnodes :vnodes="vnodes"/>
  </div>
</some-component>
components: {
  Vnodes: {
    functional: true,
    render: (h, ctx) => ctx.props.vnodes
  }
}

【讨论】: