【发布时间】:2021-02-10 18:46:46
【问题描述】:
我已经设置了复制库:
https://github.com/devidwong/recurring-slot
我只使用 Vue 运行时,所以我不能附加 Vue.extend 的小提琴(需要模板编译器)
Vue 3 是相当新的东西。我将其用于探索目的。我不知道这是否适用于 Vue 2,但我只是想知道这个组件是否应该正常工作:
comment.template.html:
<div class="comment">
<slot :currentComment="comment"></slot>
<Comment v-for="reply in comment.replies" :key="reply.id" :comment="reply">
<slot :currentComment="reply"></slot>
</Comment>
</div>
用法:
cmets.template.html
<Comment v-for="comment in comments" :key="comment.id" :comment="comment">
<template #default="{ currentComment }">
<div>by {{ currentComment.author }}</div>
</template>
</Comment>
对于结构:
comments: [
{
id: 1,
author: 'Goodman',
replies: [
{
id: 11,
author: 'RepeatingMan',
replies: [
{
id: 111,
author: 'ExpectedMan',
replies: [
{
id: 1111,
author: 'MelodyOfFuture',
replies: []
}
]
}
]
}
]
}
]
我得到 Repeatimg man 渲染 3 次,而不是 ExpectedMan 和 MelodyOfFuture。在我看来,第二个插槽与第一个插槽获得相同的评论道具。
我希望有嵌套的 cmets 并只定义一次内部的东西。
这可能吗?
【问题讨论】:
-
你想把作者显示在另一个之下吗?
-
是的。如您所见,结构很好,但数据始终来自同一级别
-
看看this有没有帮助
-
很好,你指出我的代码和框,但如果你发布解决方案 - 你的评论组件不会重复
标签: javascript vue.js vuejs2 vue-component vuejs3