要实现评论回复功能,可以将评论和回复的数据结构设计成树状结构。
首先,在评论数据结构中,可以添加一个字段用来存储回复的列表,例如:
code
comments: [
{
id: 1,
content: "评论内容",
replies: [
{
id: 11,
content: "回复内容",
replyTo: "评论者的名称",
},
// 其他回复对象
]
},
// 其他评论对象
]
然后,在评论页面中,使用v-for指令遍历评论列表并显示评论内容。对于每个评论对象,可以使用v-for指令遍历回复列表,并显示回复内容。
在添加回复的功能中,可以通过点击回复按钮打开回复弹窗,然后在弹窗中输入回复内容并提交。提交时,可以根据回复对象的id,在评论列表中找到对应的评论对象,然后将新的回复对象添加到该评论对象的回复列表中。
具体实现代码可以参考以下示例:
html
<template>
<view class="comment">
<view class="box u-row-between">
<view>
<input v-model="comment" placeholder="说点什么吧......" class="input"></input>
</view>
<view @click="show = !show">
<u-icon name="chat" labelPos="bottom" label="10" size="40" labelSize="22"></u-icon>
</view>
</view>
</view>
<!-- 评论弹窗 -->
<u-popup v-model="show" mode="bottom" length="70%" border-radius="24">
<view class="u-p-30" v-for="(comment, index) in comments" :key="comment.id">
<view class="u-flex u-col-top">
<view class="u-p-r-20">
<u-avatar text="MK" bg-color="#2BC3C8"></u-avatar>
</view>
<view class="u-flex-1">
<view class="u-tips-color u-font-sm">MK</view>
<view>{{ comment.content }}</view>
<view class="u-tips-color u-font-sm u-m-t-10">18小时前 广东 回复</view>
</view>
<view>
<u-icon name="thumb-up" size="36"></u-icon>
</view>
</view>
<!-- 回复列表 -->
<view class="u-m-t-20" v-for="(reply, replyIndex) in comment.replies" :key="reply.id">
<view class="u-flex u-col-top">
<view class="u-p-r-20">
<u-avatar text="回复者的名称首字母" bg-color="#2BC3C8"></u-avatar>
</view>
<view class="u-flex-1">
<view class="u-tips-color u-font-sm">{{ reply.replyTo }}</view>
<view>{{ reply.content }}</view>
<view class="u-tips-color u-font-sm u-m-t-10">回复时间</view>
</view>
<view>
<u-icon name="thumb-up" size="36"></u-icon>
</view>
</view>
</view>
<!-- 回复输入框 -->
<input v-model="replies[index]" placeholder="回复内容" class="input"></input>
<button @click="addReply(index)">回复</button>
</view>
</u-popup>
</template>
<script>
export default {
data() {
return {
show: false,
comment: "",
comments: [
{
id: 1,
content: "评论内容 1",
replies: [
{
id: 11,
content: "回复内容 1-1",
replyTo: "评论者的名称",
},
{
id: 12,
content: "回复内容 1-2",
replyTo: "评论者的名称",
},
],
}
],
replies: [],
};
},
methods: {
addReply(index) {
const replyContent = this.replies[index];
if (replyContent) {
const reply = {
id: this.comments[index].replies.length + 1,
content: replyContent,
replyTo: "当前登录用户的名称",
};
this.comments[index].replies.push(reply);
this.replies[index] = "";
}
},
},
};
</script>
注意,上述代码只是一个简单的示例,实际应用中可能还需要考虑更多的交互细节和逻辑处理。根据具体的需求,可以进行相应的调整和扩展。