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>

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

(0)

相关推荐

  • Vue.js实现文章评论和回复评论功能

    本来想把这个页面用jade渲染出来.评论部分用vue,但是想了想觉得麻烦,最后还是整个用vue的组件搞定他吧. 先上在线demo:http://jsbin.com/ceqifo/1/edit?js,output 再上效果图 可直接评论,点击别人的评论能回复别人的评论. html <div id="comment"> <article-content v-bind:article="article"></article-content&g

  • vue组件实现发表评论功能

    本文实例为大家分享了vue组件实现发表评论的具体代码,供大家参考,具体内容如下 今天看了vue相关的视频,所以跟着做一个小demo把知识串联起来,内容很简单但是也算是学习道路上的一点进步. 1 思路分析 发表评论模块写入一个组件,提高复用性.关键点: 1).子组件通过localStorage向父组件传值2).子组件有自己的data存储user和content,即评论人和评论内容,也就是dom元素绑定的数据.3).点击‘发表评论’后,首先是将各条评论存入localStorage,然后通过在组件出绑

  • Vue组件实现评论区功能

    本文实例为大家分享了Vue组件实现评论区的具体代码,供大家参考,具体内容如下 实现代码 <!DOCTYPE html> <html lang="en"> <head>     <meta charset="utf-8">     <meta name="viewport" content="width=device-width, inital-scale=1.0">

  • vue实现发表评论功能

    本文实例为大家分享了vue实现发表评论的具体代码,供大家参考,具体内容如下 <!DOCTYPE html> <html>     <head>         <meta charset="utf-8">         <link rel="stylesheet" href="bootstrap-3.3.7.css" />         <title></title

  • VUE+Java实现评论回复功能

    背景 最近需要做一个多级评论的功能,技术路线:VUE(Element)+Java(SpringBoot) 效果 后台 SQL Java Controller /**   * 根据关联id获取评论信息   * @param relationId 关联id   * @param type 类型   * @return: com.harvey.result.ResultSupport<java.lang.Object>   * @date: 2020/12/10 14:37   */  @GetMa

  • vue开发实现评论列表

    本文实例为大家分享了vue开发实现评论列表的具体代码,供大家参考,具体内容如下 index.html <!DOCTYPE html> <html>   <head>     <meta charset="utf-8">     <meta name="viewport" content="width=device-width,initial-scale=1.0">     <lin

  • Vue实现简单的发表评论功能

    本文实例为大家分享了Vue实现简单的发表评论功能的具体代码,供大家参考,具体内容如下 1.这是我在学习中的实例,有些的不足的地方,还望各位大佬指点,感谢哦~ 2.发表评论的效果图 点击"发表"之后的效果(每条评论之后点击"删除"可以删掉这一整条评论~) 3.完整代码展示(我html结构写的比较乱,这里提醒大家一下,没有定义类的div是可以删掉的,我是因为方便写样式所以多加了div) 还是要提醒一下,不要忘记引入vue.js,目录记得根据自己存放的位置改 <!D

  • Vuepress 搭建带评论功能的静态博客的实现

    vuepress 是 Vue 驱动的静态站点生成工具 本文仅介绍,搭建静态博客的过程,具体教程及文档请点击进入 vuepress中文网 点击查看项目代码 vuepress初始化 下面初始化 # 将 github 新创建的仓库克隆到本地 git clone git@github.com:zhb333/readme-blog.git # 进入项目 cd readme-blog # npm 初始化, 按照提示回车 npm init # 安装 vuepress npm i vuepress -D # 安

  • vue实现评论列表

    本文实例为大家分享了vue实现评论列表的具体代码,供大家参考,具体内容如下 案例数据使用localStorage持久性存储 全局过滤器实现时间格式化 代码部分 <!DOCTYPE html> <html lang="zh">     <head>         <meta charset="UTF-8">         <meta name="viewport" content="

  • vue实现评论列表功能

    具体代码如下所示: <!DOCTYPE html> <html> <head> <title>简易评论列表</title> <meta charset="utf-8"> <link rel="stylesheet" href="node_modules\bootstrap\dist\css\bootstrap.css" rel="external nofoll

随机推荐