v-for循环用户评论点击查看回复只显示当前评论的回复
可以使用v-for循环来遍历用户评论,并使用v-if指令来判断是否显示当前评论的回复。具体的实现步骤如下:
- 在数据中定义一个数组来存储用户评论和对应的回复,例如comments数组。
- 使用v-for指令来遍历comments数组,将每个评论显示出来。
- 在每个评论中添加一个按钮或链接,用来触发显示该评论的回复。
- 使用v-if指令来判断是否显示当前评论的回复,当点击按钮或链接时,将对应的回复显示出来。
示例代码如下:
<template>
<div>
<div v-for="(comment, index) in comments" :key="index">
<p>{{ comment.text }}</p>
<button @click="showReply(index)">查看回复</button>
<div v-if="comment.showReply">
<p v-for="(reply, replyIndex) in comment.replies" :key="replyIndex">
{{ reply.text }}
</p>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
comments: [
{
text: '评论1',
showReply: false,
replies: [
{
text: '回复1'
},
{
text: '回复2'
}
]
},
{
text: '评论2',
showReply: false,
replies: [
{
text: '回复3'
},
{
text: '回复4'
}
]
}
]
}
},
methods: {
showReply(index) {
this.comments[index].showReply = !this.comments[index].showReply;
}
}
}
</script>
在上面的代码中,comments数组存储了用户评论和对应的回复。在每个评论中,使用v-if指令来判断是否显示回复,当点击"查看回复"按钮时,会调用showReply方法来切换显示回复的状态
原文地址: https://www.cveoy.top/t/topic/imUY 著作权归作者所有。请勿转载和采集!