当前位置:首页 > VUE

vue实现抖音评论

2026-02-20 17:12:16VUE

Vue 实现抖音评论功能

数据结构设计

评论数据通常采用嵌套结构,包含主评论和子评论(回复)。示例数据结构:

comments: [
  {
    id: 1,
    userId: 101,
    avatar: 'user1.jpg',
    username: '用户A',
    content: '这个视频太棒了!',
    timestamp: '10分钟前',
    likes: 24,
    replies: [
      {
        id: 11,
        userId: 102,
        avatar: 'user2.jpg',
        username: '用户B',
        content: '我也觉得',
        timestamp: '5分钟前',
        likes: 5
      }
    ]
  }
]

评论列表渲染

使用 v-for 渲染嵌套评论,通过递归组件处理子评论:

vue实现抖音评论

<template>
  <div class="comment-list">
    <div v-for="comment in comments" :key="comment.id" class="comment-item">
      <img :src="comment.avatar" class="avatar">
      <div class="content">
        <span class="username">{{ comment.username }}</span>
        <p>{{ comment.content }}</p>
        <div class="meta">
          <span>{{ comment.timestamp }}</span>
          <button @click="likeComment(comment.id)">赞 {{ comment.likes }}</button>
        </div>

        <!-- 子评论 -->
        <div v-if="comment.replies.length" class="reply-list">
          <CommentItem v-for="reply in comment.replies" :comment="reply"/>
        </div>
      </div>
    </div>
  </div>
</template>

发表评论功能

实现评论输入框和提交逻辑:

<template>
  <div class="comment-input">
    <input 
      v-model="newComment" 
      placeholder="说点什么..."
      @keyup.enter="submitComment"
    >
    <button @click="submitComment">发送</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      newComment: ''
    }
  },
  methods: {
    submitComment() {
      if (!this.newComment.trim()) return

      const comment = {
        id: Date.now(),
        userId: currentUserId,
        content: this.newComment,
        timestamp: '刚刚',
        likes: 0,
        replies: []
      }

      this.$emit('add-comment', comment)
      this.newComment = ''
    }
  }
}
</script>

点赞功能实现

为评论添加点赞交互:

vue实现抖音评论

methods: {
  likeComment(commentId) {
    const comment = findCommentById(this.comments, commentId)
    if (comment) {
      comment.likes += comment.liked ? -1 : 1
      comment.liked = !comment.liked
    }
  }
}

无限滚动加载

结合 IntersectionObserver 实现滚动加载:

mounted() {
  const observer = new IntersectionObserver((entries) => {
    if (entries[0].isIntersecting) {
      this.loadMoreComments()
    }
  })
  observer.observe(this.$refs.loadMoreTrigger)
}

样式优化

抖音风格的 CSS 关键点:

.comment-list {
  max-height: 60vh;
  overflow-y: auto;
  scrollbar-width: none; /* 隐藏滚动条 */
}

.comment-item {
  display: flex;
  padding: 8px 0;
  border-bottom: 1px solid #eee;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  margin-right: 12px;
}

性能优化建议

  1. 使用虚拟滚动处理大量评论
  2. 对频繁更新的点赞数使用防抖
  3. 图片懒加载
  4. 分页加载评论数据

完整组件示例

<template>
  <div class="comment-container">
    <CommentInput @add-comment="addComment"/>
    <CommentList :comments="comments" @like="handleLike"/>
  </div>
</template>

<script>
export default {
  data() {
    return {
      comments: [] // 从API获取初始数据
    }
  },
  methods: {
    addComment(comment) {
      this.comments.unshift(comment)
    },
    handleLike(commentId) {
      // 更新点赞状态
    }
  }
}
</script>

标签: vue
分享给朋友:

相关文章

vue如何实现放大缩小

vue如何实现放大缩小

Vue实现放大缩小功能的方法 在Vue中实现元素的放大缩小功能,可以通过CSS变换、第三方库或直接操作DOM样式来实现。以下是几种常见的方法: 使用CSS transform属性 通过绑定Vue的d…

vue 实现关闭

vue 实现关闭

Vue 实现关闭功能的方法 在Vue中实现关闭功能通常涉及组件销毁、模态框隐藏或路由跳转等场景。以下是几种常见实现方式: 销毁当前组件 使用v-if或v-show控制组件显示/隐藏,适用于局部组件:…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue实现删除

vue实现删除

Vue 实现删除功能的方法 在 Vue 中实现删除功能通常涉及以下几个步骤: 数据绑定与列表渲染 使用 v-for 指令渲染列表数据,并为每个项绑定唯一标识符(如 id)。例如: <…

vue 实现列表

vue 实现列表

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

vue实现suspense

vue实现suspense

Vue 3 中实现 Suspense Suspense 是 Vue 3 引入的一个内置组件,用于处理异步依赖的加载状态。它允许在等待异步组件或异步数据加载时显示一个后备内容(fallback),直到异…