Vue + semanticUI + Java 实现多级评论

交友

  • 背景: 最近在弄一个博客网站,前端是vue + semanticUI ,后台用的springboot。在弄到评论这一块的时候,不知道级联嵌套评论怎么搞,从上午想到现在想出来了,特来记录一下
后台

后台是通过树来实现的。
数据库的表结构如下所示:
在这里插入图片描述
需要做的就是通过这个表,构建一个树,java代码如下:

package cn.zi10ng.blog.util;

import cn.zi10ng.blog.domain.CommentInfo;
import cn.zi10ng.blog.domain.Node;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Zi10ng
 * @date 2019年8月24日21:20:16
 */
public class TreeUtils {

    /**
     * 把评论信息的集合转化为一个树
     */
    public Node buildTree(List<CommentInfo> commentInfo, long id){
        Node tree = new Node();
        List<Node> children = new ArrayList<>();
        List<Node> nodeList = new ArrayList<>();
        for (CommentInfo info : commentInfo) {
            children.add(buildNode(info));
        }
        tree.setId(id);
        tree.setChildren(children);
        for (Node child : children) {
            Node node = findNode(children, child.getParentId());
            List<Node> nodes = new ArrayList<>();
            if (node != null) {
                if (node.getChildren() != null) {
                    nodes = node.getChildren();
                    nodes.add(child);
                    node.setChildren(nodes);
                }else {
                    nodes.add(child);
                    node.setChildren(nodes);
                }
                nodeList.add(child);
            }
        }
        for (Node node : nodeList) {
            children.remove(node);
        }
        return tree;
    }

    private Node findNode(List<Node> nodes, long id){
        for (Node node : nodes) {
            if (node.getId() == id) {
                return node;
            }
        }
        return null;
    }

    private Node buildNode(CommentInfo info){
        Node node = new Node();
        node.setId(info.getId());
        node.setParentId(info.getParentId());
        node.setObject(info);
        node.setChildren(null);

        return node;
    }
}

前端

前端我用的semanticUI这个css和vue,现在我们主要的任务就是把后端的树结构在前端展示出来

因为对前端不是太熟,如果有前端dalao看见不妨指点一二

  • 纯css如下:
      <div class="comment" v-for="articleComment in articleCommentTree.children" :key="articleComment.object.id">
          <a class="avatar">
            <img :src="imgSrc" alt="头像">
          </a>
          <div class="content">
            <a class="author">{{articleComment.object.name}}</a>
            <div class="metadata">
              <span class="date">{{articleComment.object.createByStr}}</span>
            </div>
            <div class="text">{{articleComment.object.content}} </div>
            <div class="actions">
              <a class="reply">回复</a>
            </div>
          </div>
            <div v-if="articleComment.children != null" class="comments">
    			<div class="comment" v-for="subArticleComment in children" :key="subArticleComment.object.id">
      				<a class="avatar">
        				<img :src="imgSrc" alt="头像">
      				</a>
      			<div class="content">
        			<a class="author">{{subArticleComment.object.name}}</a>
        			<div class="metadata">
          			<span class="date">{{subArticleComment.object.createByStr}}</span>
       			</div>
        		<div class="text">{{subArticleComment.object.content}} </div>
        		<div class="actions">
          		<a class="reply">回复</a>
       		 </div>
      		</div>
    </div>
  </div>
</div>

如大家所见,这样只是一个死的静态模板,最多只能有二级评论,那么如何利用VUE实现多级评论呢?
这里主要还是用了递归,对于一个后端狗来说,只知道原理但却不知如何在前端实现是最痛苦的QAQ

解决方法就是把多级评论封装为一个组件,这样就可以了,代码如下:

  • 组件
<template>
  <div class="multistage">
  <div v-if="children != null" class="comments">
    <div class="comment" v-for="subArticleComment in children" :key="subArticleComment.object.id">
      <a class="avatar">
        <img :src="imgSrc" alt="头像">
      </a>
      <div class="content">
        <a class="author">{{subArticleComment.object.name}}</a>
        <div class="metadata">
          <span class="date">{{subArticleComment.object.createByStr}}</span>
        </div>
        <div class="text">{{subArticleComment.object.content}} </div>
        <div class="actions">
          <a class="reply">回复</a>
        </div>
      </div>
      <multistage :children="subArticleComment.children"/>
    </div>
  </div>
  </div>
</template>

<script>
export default {
  name: 'multistage',
  props: ['children'],
  data () {
    return {
      imgSrc: 'https://picsum.photos/id/234/100/100'
    }
  }
}
</script>

<style scoped>

</style>

  • 模板调用:
<div class="comment" v-for="articleComment in articleCommentTree.children" :key="articleComment.object.id">
          <a class="avatar">
            <img :src="imgSrc" alt="头像">
          </a>
          <div class="content">
            <a class="author">{{articleComment.object.name}}</a>
            <div class="metadata">
              <span class="date">{{articleComment.object.createByStr}}</span>
            </div>
            <div class="text">{{articleComment.object.content}} </div>
            <div class="actions">
              <a class="reply">回复</a>
            </div>
          </div>
           <multistage :children="articleComment.children"/>
        </div>

这样就OK了

在这里插入图片描述

  • 7
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
Vue Element UI中实现评论功能需要依赖一些组件,如表单、输入框、按钮、弹窗等。下面是一个简单的评论功能实现步骤: 1. 引入Element UI组件库 在Vue项目中,可以通过npm安装Element UI组件库,并在main.js文件中引入。 ``` npm install element-ui --save import Vue from 'vue'; import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI); ``` 2. 创建评论组件 在Vue组件中,可以创建一个评论组件,用于显示评论列表和评论表单。在该组件中,需要定义data属性,用于存储评论列表和表单数据。 ``` <template> <div> <div v-for="comment in comments" :key="comment.id">{{comment.content}}</div> <el-form :model="form" label-position="left" label-width="80px" inline> <el-form-item label="评论"> <el-input v-model="form.content" placeholder="请输入评论内容"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="addComment">发表评论</el-button> </el-form-item> </el-form> </div> </template> <script> export default { data() { return { comments: [], form: { content: '' } } }, methods: { addComment() { const comment = { id: Date.now(), content: this.form.content } this.comments.push(comment) this.form.content = '' } } } </script> ``` 在上面的代码中,el-form表示表单,el-input表示输入框,el-button表示按钮。comments数组用于存储评论列表,form对象用于存储表单数据。addComment方法用于发表评论,将表单数据存储到comments数组中,并清空表单数据。 3. 在父组件中使用评论组件 在父组件中使用评论组件,并将评论列表传递给该组件。 ``` <template> <div> <comment :comments="comments"></comment> </div> </template> <script> import Comment from './Comment.vue' export default { data() { return { comments: [ {id: 1, content: '这是一条评论'}, {id: 2, content: '这是另一条评论'} ] } }, components: { Comment } } </script> ``` 在上面的代码中,comment组件通过属性comments获取评论列表,并在父组件中定义comments数组,存储评论数据。 4. 给评论组件添加弹窗 在评论组件中,可以添加一个弹窗组件,用于提示评论成功。 ``` <template> <div> <div v-for="comment in comments" :key="comment.id">{{comment.content}}</div> <el-form :model="form" label-position="left" label-width="80px" inline> <el-form-item label="评论"> <el-input v-model="form.content" placeholder="请输入评论内容"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="addComment">发表评论</el-button> </el-form-item> </el-form> <el-dialog title="提示" :visible.sync="dialogVisible"> <p>评论成功!</p> </el-dialog> </div> </template> <script> export default { data() { return { comments: [], form: { content: '' }, dialogVisible: false } }, methods: { addComment() { const comment = { id: Date.now(), content: this.form.content } this.comments.push(comment) this.form.content = '' this.dialogVisible = true setTimeout(() => { this.dialogVisible = false }, 2000) } } } </script> ``` 在上面的代码中,el-dialog表示弹窗,dialogVisible属性用于控制弹窗的显示/隐藏。在addComment方法中,添加评论成功后,弹出评论成功的提示,并在2秒后关闭弹窗。 这就是一个简单的Vue Element UI评论功能实现

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值