当前位置:首页 > VUE

vue实现多层评论回复

2026-02-09 13:49:44VUE

Vue 实现多层评论回复

数据结构设计

采用嵌套结构存储评论数据,每条评论包含子评论数组:

comments: [
  {
    id: 1,
    content: '一级评论',
    replies: [
      {
        id: 11,
        content: '二级回复',
        replies: [
          { id: 111, content: '三级回复' }
        ]
      }
    ]
  }
]

递归组件实现

创建可递归调用的评论组件:

<template>
  <div class="comment">
    <div>{{ comment.content }}</div>
    <button @click="replyTo(comment.id)">回复</button>

    <div v-if="showReplyForm === comment.id">
      <textarea v-model="replyContent"></textarea>
      <button @click="submitReply">提交</button>
    </div>

    <div class="replies" v-if="comment.replies">
      <comment-item 
        v-for="reply in comment.replies"
        :key="reply.id"
        :comment="reply"
        @reply="handleReply"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: 'CommentItem',
  props: ['comment'],
  data() {
    return {
      showReplyForm: null,
      replyContent: ''
    }
  },
  methods: {
    replyTo(id) {
      this.showReplyForm = id
    },
    submitReply() {
      this.$emit('reply', {
        parentId: this.comment.id,
        content: this.replyContent
      })
      this.replyContent = ''
      this.showReplyForm = null
    },
    handleReply(payload) {
      this.$emit('reply', payload)
    }
  }
}
</script>

数据操作方法

在父组件中实现添加回复的逻辑:

methods: {
  addReply({ parentId, content }) {
    const newReply = {
      id: Date.now(),
      content,
      replies: []
    }

    function findAndAppend(comments) {
      for (let comment of comments) {
        if (comment.id === parentId) {
          comment.replies.push(newReply)
          return true
        }
        if (comment.replies && comment.replies.length) {
          if (findAndAppend(comment.replies)) return true
        }
      }
      return false
    }

    findAndAppend(this.comments)
  }
}

样式优化

添加缩进样式显示层级关系:

.comment {
  margin-left: 20px;
  padding: 10px;
  border-left: 2px solid #eee;
}

.replies {
  margin-top: 10px;
}

完整示例调用

父组件调用方式:

<template>
  <div>
    <comment-item 
      v-for="comment in comments"
      :key="comment.id"
      :comment="comment"
      @reply="addReply"
    />
  </div>
</template>

<script>
import CommentItem from './CommentItem.vue'

export default {
  components: { CommentItem },
  data() {
    return {
      comments: [] // 初始评论数据
    }
  }
}
</script>

vue实现多层评论回复

标签: 多层vue
分享给朋友:

相关文章

vue实现条件判断

vue实现条件判断

Vue 条件判断实现方法 Vue 提供了多种方式实现条件判断,可以根据不同场景选择合适的方式。 v-if / v-else-if / v-else 这是 Vue 中最常用的条件渲染指令,根据表达式的…

vue实现modal

vue实现modal

Vue 实现 Modal 弹窗组件 使用 Vue 实现 Modal 弹窗组件可以通过多种方式,包括原生 Vue 组件、第三方库或自定义指令。以下是几种常见方法: 原生 Vue 组件实现 创建一个基…

vue 实现列表

vue 实现列表

Vue 实现列表的方法 在 Vue 中实现列表通常使用 v-for 指令,结合数组或对象数据渲染动态列表。以下是几种常见的实现方式: 基础列表渲染 使用 v-for 指令遍历数组,渲染列表项。ite…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json):…

vue 实现流程

vue 实现流程

Vue 实现流程 Vue.js 是一个渐进式 JavaScript 框架,用于构建用户界面。以下是 Vue 实现的基本流程: 安装 Vue.js 通过 CDN 引入或使用 npm/yarn 安装:…

利用vue 实现

利用vue 实现

以下是利用 Vue 实现常见功能的几种方法,分为不同场景和技术要点: 数据绑定与响应式更新 Vue 的核心特性是数据绑定,通过 v-model 实现双向绑定: <template>…