javascript - Vue 中可能有重复的插槽吗?
问题描述
我已经设置了复制存储库:
https://github.com/devidwong/recurring-slot
我只使用 Vue 运行时,所以我不能附加小提琴Vue.extend
(需要模板编译器)
Vue 3 是相当新的东西。我将其用于探索目的。我不知道这是否适用于 Vue 2,但我只是想知道这个组件是否应该工作:
评论.模板.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>
用法:
comments.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
. 在我看来,第二个插槽与第一个插槽获得相同的评论道具。我希望有嵌套的评论并只定义一次内部的东西。
这可能吗?
解决方案
不要通过 slot 再次将评论暴露给父组件,只需在Comment
Component 中打印作者姓名和评论:
<div class="comment">
{{comment.author}}
<Comment v-for="reply in comment.replies" :key="reply.id" :comment="reply">
<slot :currentComment="reply"></slot>
</Comment>
</div>
父组件:
<Comment v-for="comment in comments" :key="comment.id" :comment="comment">
</Comment>
在现场演示中,我尝试模拟 stackoverflow 评论风格。
推荐阅读
- php - 1 分钟后无法发送推送通知
- kuzzle - 使用钩子或订阅在 Kuzzle 中发送推送通知的更好方法是什么?
- java - 如何在没有打印语句的情况下扫描多行
- python-3.x - 在 Python3 中使用 ip_address 从 ipaddress 获取网络
- python - ModuleNotFoundError:没有名为“art.attacks”的模块
- firebase - 如何将firebase项目的身份验证用于其他项目的firestore?
- python - 我该如何解决这个 python 问题?[django]
- python-3.x - 如何检查 Python 程序在运行时使用了多少内存
- javascript - 使用百分比查找范围内的值
- java - 如何将对象从活动发送到片段?