vue实现文章评论和回复列表

 更新时间:2022年04月14日 15:31:52   作者:nao儿  
这篇文章主要为大家详细介绍了vue实现文章评论和回复列表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了vue实现文章评论和回复列表的具体代码,供大家参考,具体内容如下

效果预览:

父组件:

<template>
  <div class="comment-reply">
    <div
      v-for="(item, index) in articleLists"
      :key="index"
      class="article-list"
    >
      <div class="article-desc">{{ item.articleDesc }}</div>
      <div v-if="item.children.length > 0">
        <div class="reply-list" v-if="item.children.length > 0">
          <my-cycle-list
            v-for="(comment, index) in item.children"
            :self="comment"
            :parent="comment"
            :key="index"
          ></my-cycle-list>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import myCycleList from '@/components/my-cycle-list'
export default {
  components: { myCycleList },
  data() {
    return {
      // 文章列表
      articleLists: [
        { articleId: 'article-1', articleDesc: '围城' },
        { articleId: 'article-2', articleDesc: '骆驼祥子' },
        { articleId: 'article-3', articleDesc: '边城' },
        { articleId: 'article-4', articleDesc: '朝花夕拾' }
      ],
      // 评论列表
      commentsList: [
        {
          userId: 'user-1',
          userName: '赵一',
          articleId: 'article-1', // 关联的文章id
          commentId: 'comment-1', // 评论id
          replyId: null, // 回复哪条评论的id
          desc: '作者是谁',
          time: '2021-04-05 15:30:25'
        },
        {
          userId: 'user-2',
          userName: '钱二',
          articleId: 'article-1',
          commentId: 'comment-2',
          replyId: null,
          desc: '对呀,作者也不写',
          time: '2021-04-05 15:30:25'
        },
        {
          userId: 'user-3',
          userName: '孙三',
          articleId: 'article-1',
          commentId: 'comment-3',
          replyId: null,
          desc: '楼上俩初中没毕业吧',
          time: '2021-04-05 15:30:25'
        },
        {
          userId: 'user-4',
          userName: '李四',
          articleId: 'article-1',
          commentId: 'comment-4',
          replyId: 'comment-1',
          desc: '作者是钱钟书',
          time: '2021-04-05 15:30:25'
        },
        {
          userId: 'user-9',
          userName: '牛九',
          articleId: 'article-1',
          commentId: 'comment-10',
          replyId: 'comment-1',
          desc: '钱钟书',
          time: '2021-04-05 15:30:25'
        },
        {
          userId: 'user-5',
          userName: '王五',
          articleId: 'article-2',
          commentId: 'comment-5',
          replyId: null,
          desc: '哈哈哈',
          time: '2021-04-05 15:30:25'
        },
        {
          userId: 'user-6',
          userName: '张六',
          articleId: 'article-1',
          commentId: 'comment-6',
          replyId: 'comment-4',
          desc: '不对吧',
          time: '2021-04-05 15:30:25'
        },
        {
          userId: 'user-7',
          userName: '顾七',
          articleId: 'article-1',
          commentId: 'comment-7',
          replyId: 'comment-6',
          desc: '对的,就是钱钟书',
          time: '2021-04-05 15:30:25'
        },
        {
          userId: 'user-8',
          userName: '朱八',
          articleId: 'article-3',
          commentId: 'comment-8',
          replyId: null,
          desc: '这本书真不错',
          time: '2021-04-05 15:30:25'
        },
        {
          userId: 'user-9',
          userName: '纪九',
          articleId: 'article-4',
          commentId: 'comment-9',
          replyId: null,
          desc: '真的好看',
          time: '2021-04-05 15:30:25'
        }
      ]
    }
  },
  created() {
    this.initCommentLists()
    this.initArticleLists()
  },
  methods: {
    // 格式化评论数据
    initCommentLists() {
      this.commentsList.forEach(i => {
        this.$set(i, 'children', [])
        // 将回复该评论的列表放入children中
        let filterList = this.commentsList.filter(
          j => j.replyId === i.commentId
        )
        if (filterList.length > 0) {
          i.children = filterList
        }
      })
      // 过滤出最高级
      this.commentsList = this.commentsList.filter(i => i.replyId === null)
    },
    // 格式化文章数据
    initArticleLists() {
      this.articleLists.forEach(i => {
        this.$set(i, 'children', [])
        let filterList = this.commentsList.filter(
          j => j.articleId === i.articleId
        )
        if (filterList.length > 0) {
          i.children = filterList
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">
.comment-reply {
  .article-list {
    margin: 15px;
    .article-desc {
      color: coral;
      font-size: 26px;
      font-weight: bold;
    }
  }
  .comment-list {
    margin: 10px;
    .comment {
      .comment-username {
        color: #999;
        cursor: pointer;
      }
    }
  }
}
</style>

my-cycle-list组件:

<template>
  <div class="my-cycle-list">
    <div class="lists">
      <!-- 回复 -->
      <div v-if="self.replyId">
        <span class="self-username"> {{ self.userName }} 回复 </span>
        <span class="parent-username" @click="parentClick"
          >{{ parent.userName }}:</span
        >
        {{ self.desc }}
        <span class="time">{{ self.time }}</span>
      </div>
      <!-- 评论 -->
      <div v-else>
        <span class="self-username" @click="commentUserNameClick">
          {{ self.userName }}:
        </span>
        {{ self.desc }}
        <span class="time">{{ self.time }}</span>
      </div>
      <!-- 递归组件 -->
      <div v-if="self.children.length < flagNum || showAll">
        <my-cycle-list
          v-for="(child, index) in self.children"
          :self="child"
          :parent="self"
          :key="index"
        ></my-cycle-list>
      </div>
      <!-- 查看全部 -->
      <div
        v-if="self.children.length >= flagNum"
        class="view-all"
        @click="viewAll"
      >
      {{ !showAll ? `查看全部 ${self.children.length} 条回复` : `收起 ${self.children.length} 条回复`}}
         
      </div>
    </div>
  </div>
</template>
<script>
import myCycleList from '@/components/my-cycle-list'
export default {
  props: ['self', 'parent'],
  components: { myCycleList },
  name: 'my-cycle-list',
  data() {
    return {
      flagNum: 2, // 超过多少条折叠
      showAll: false
    }
  },
  created() {},
  methods: {
    // 点击被回复的昵称事件
    parentClick() {
      console.log(this.parent)
    },
    // 评论人点击事件
    commentUserNameClick() {
      console.log(this.self)
    },
    // 查看/收起回复
    viewAll() {
      this.showAll = !this.showAll
    }
  }
}
</script>
<style scoped lang="scss">
.my-cycle-list {
  .lists {
    margin: 10px;
    .self-username {
      cursor: pointer;
      color: #999;
    }
    .parent-username {
      color: burlywood;
      cursor: pointer;
    }
    .time {
      margin: 0 10px;
      color: #666;
      font-size: 12px;
    }
    .view-all {
      margin: 10px 0;
      color: darkcyan;
      cursor: pointer;
    }
  }
}
</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Vue中watch监听属性新旧值相同的问题解决方案

    Vue中watch监听属性新旧值相同的问题解决方案

    这篇文章主要给大家分享了Vue中watch监听属性新旧值相同问题解决方案,如果有遇到相同问题的朋友,可以参考阅读本文
    2023-08-08
  • vue实现拖拽的简单案例 不超出可视区域

    vue实现拖拽的简单案例 不超出可视区域

    这篇文章主要为大家详细介绍了vue实现拖拽的简单案例,不超出可视区域,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • 解决vue中修改了数据但视图无法更新的情况

    解决vue中修改了数据但视图无法更新的情况

    今天小编就为大家分享一篇解决vue中修改了数据但视图无法更新的情况,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • VUE引入腾讯地图并实现轨迹动画的详细步骤

    VUE引入腾讯地图并实现轨迹动画的详细步骤

    这篇文章主要介绍了VUE引入腾讯地图并实现轨迹动画,引入步骤大概是在 html 中通过引入 script 标签加载API服务,结合实例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-09-09
  • 如何使用Vue3+elementPlus的Tree组件实现一个拖拽文件夹管理

    如何使用Vue3+elementPlus的Tree组件实现一个拖拽文件夹管理

    最近在做一个文件夹管理的功能,要实现一个树状的拖拽文件夹面板,里面包含两种元素,文件夹以及文件,这篇文章主要介绍了使用Vue3+elementPlus的Tree组件实现一个拖拽文件夹管理 ,需要的朋友可以参考下
    2023-09-09
  • Vue动态绑定class、style、background的方式

    Vue动态绑定class、style、background的方式

    文章主要介绍了在Vue.js中如何使用动态绑定class、style和background来实现动态样式和背景图的设置,通过v-bind指令,可以灵活地根据数据变化来动态更新元素的样式和背景
    2025-01-01
  • 浅谈vue项目如何打包扔向服务器

    浅谈vue项目如何打包扔向服务器

    本篇文章主要介绍了浅谈vue项目如何打包扔向服务器,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-05-05
  • Vue3纯前端实现Vue路由权限的方法详解

    Vue3纯前端实现Vue路由权限的方法详解

    这篇文章主要给大家介绍了关于Vue3纯前端实现Vue路由权限的相关资料,文中通过实例代码介绍的非常详细,对大家学习或者使用Vue3具有一定的参考学习价值,需要的朋友可以参考下
    2022-05-05
  • Vue中封装input组件的实例详解

    Vue中封装input组件的实例详解

    这篇文章主要介绍了Vue中封装input组件的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
    2017-10-10
  • Vue3实现组件二次封装的小技巧分享

    Vue3实现组件二次封装的小技巧分享

    组件的二次封装:保留组件已有的功能,需要重写组件方法,当组件已有大量功能时候,则需要重写很多重复代码,且组件功能进行修改的时候,封装的组件也需要对应修改,从而造成许多开发和维护成本,本文给大家分享了Vue3实现组件二次封装的小技巧,需要的朋友可以参考下
    2024-09-09

最新评论