【Vue3组件】分享一下自己写的简约风格评论区组件

2024-06-23 12:12

本文主要是介绍【Vue3组件】分享一下自己写的简约风格评论区组件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


代码比较简单,方便大家二次开发,旨在快速提供基础的样式模板,自行迭代定制


预览

在这里插入图片描述


简介

data = [{userImg:'',userName:"比克",time:'17小时前',content:"这生成的真不错呀!",ReplyData:[{userImg1:'',userName1:'比克大魔王',userImg2:'',userName2:'后方之水',replytime:'6小时前',replycontent:'哈哈哈,多谢夸奖!',anthTags:1},{userImg1:'',userName1:'后方之水',userImg2:'',userName2:'比克大魔王',replytime:'6小时前',replycontent:'我也要生成同款',anthTags:2},]}]
  • 使用提示

    • 确认传递给组件的数据严格遵循上述结构,以确保界面的正确显示。
    • 利用Vue 3的Composition API特性,可以进一步优化状态管理和逻辑处理。

代码

父组件

<template><div class="firstglobals"><div class="avatar"><img :src="data.userImg" style="width: 56px; height: 56px; border-radius: 50%;" alt="" /></div><div class="content"><div class="usertop"><div class="username">{{ data.userName }}</div><div class="time">{{ data.time }}</div></div><div style="display: flex; flex-direction: column; margin-top: 1em;"><div class="text">{{ data.content }}</div><div style="display: flex; align-self: flex-end;"><img src="@/assets/globals/点赞默认.png" style="width: 20px;" alt="" /></div><div class="but">回复</div></div><div><div v-for="(item, index) in displayedReplies" :key="index"><LuxCommentSectionItem :replyData="item" /></div><div v-if="!showAllReplies && data.ReplyData.length > 2" class="load-more"@click="showAllReplies = true">加载更多回复 ...</div></div></div></div>
</template><script setup lang="ts">
import { ref, computed } from 'vue';
import LuxCommentSectionItem from '@/components/currency/LuxCommentSectionItem.vue';interface ReplyData {userImg1:string,userName1:string,userImg2:string,userName2:string,replytime:string,replycontent:string,anthTags:number
}interface CommentData {userImg: string;userName: string;time: string;content: string;ReplyData: ReplyData[];
}const props = defineProps<{data: CommentData;
}>();const showAllReplies = ref(false);const displayedReplies = computed(() => {if (showAllReplies.value || props.data.ReplyData.length <= 2) {return props.data.ReplyData;} else {return props.data.ReplyData.slice(0, 2);}
});
</script><style scoped>
.but {width: 60px;padding: 5px 0px;background: #f1f1f1;border-radius: 4px;display: flex;justify-content: center;align-content: center;font-weight: 400;font-size: 14px;color: #0f1014;text-align: left;font-style: normal;text-transform: none;
}.but:hover {background: #ebe4e4;
}.text {font-weight: 400;font-size: 18px;color: #000000;line-height: 21px;text-align: left;font-style: normal;text-transform: none;
}.time {font-weight: 400;font-size: 12px;color: #666666;line-height: 14px;text-align: left;font-style: normal;text-transform: none;
}.avatar {width: 56px;height: 56px;border-radius: 30px;
}.usertop {display: flex;flex-direction: column;gap: 5px;
}.username {font-weight: 700;font-size: 16px;color: #0f1014;line-height: 19px;text-align: left;font-style: normal;text-transform: none;
}.content {display: flex;flex-direction: column;margin-left: 1em;margin-top: 10px;flex: 1;
}.firstglobals {display: flex;justify-content: start;margin-top: 2em;
}.load-more {margin-top: 30px;margin-left: 2em;color: #0066cc;cursor: pointer;font-size: 14px;font-size: 14px;cursor: pointer;
}.load-more:hover {text-decoration: underline;}
</style>

子组件

<template><div class="reply-comments"><div class="top-user"><div style="display: flex;justify-content: center;align-content: center;gap: 8px;"><img :src="replyData.userImg1" style="width: 24px;height: 24px;border-radius: 50%;" alt=""><span class="username">{{ replyData.userName1 }}</span></div><div class="tags" v-if="replyData.anthTags === 1">作者</div><div class="hf">回复</div><div style="display: flex;justify-content: center;align-content: center;gap: 8px;"><img :src="replyData.userImg2" style="width: 24px;height: 24px;border-radius: 50%;" alt=""><span class="username">{{ replyData.userName2 }}</span></div><div class="tags" v-if="replyData.anthTags === 2">作者</div><div class="time">{{ replyData.replytime }}</div></div><div class="content">{{ replyData.replycontent }}</div><div style="display: flex;align-self: flex-end;"><img src="@/assets/globals/点赞默认.png" style="width: 20px;" alt=""></div><div class="but">回复</div></div>
</template><script setup lang="ts">interface ReplyData {userImg1: string,userName1: string,userImg2: string,userName2: string,replytime: string,replycontent: string,anthTags: number
}const props = defineProps<{replyData: ReplyData
}>();
</script><style scoped>
.but {width: 60px;/* height: 28px; */padding: 5px 0px;background: #F1F1F1;border-radius: 4px 4px 4px 4px;display: flex;justify-content: center;align-content: center;font-weight: 400;font-size: 14px;color: #0F1014;/* line-height: 16px; */text-align: left;font-style: normal;text-transform: none;margin-left: 32px;
}.but:hover {background: #ebe4e4;
}.content {font-weight: 400;font-size: 18px;color: #000000;line-height: 21px;text-align: left;font-style: normal;text-transform: none;margin-left: 32px;margin-top: 10px;
}.time {font-weight: 400;font-size: 12px;color: #666666;line-height: 14px;text-align: left;font-style: normal;text-transform: none;
}.hf {font-weight: 400;font-size: 14px;color: #B9B9B9;line-height: 16px;text-align: left;font-style: normal;text-transform: none;
}.tags {width: 32px;height: 18px;background: #0F1014;border-radius: 4px 4px 4px 4px;color: #fff;font-weight: 400;font-size: 10px;color: #FFFFFF;line-height: 12px;text-align: center;font-style: normal;text-transform: none;flex-wrap: wrap;display: flex;justify-content: center;align-items: center;
}.username {height: 24px;font-weight: 500;font-size: 13px;color: #0F1014;line-height: 15px;text-align: left;font-style: normal;text-transform: none;display: flex;flex-wrap: wrap;justify-content: center;align-items: center;
}.top-user {display: flex;align-items: center;/* flex-wrap: wrap; */gap: 8px;
}.reply-comments {display: flex;flex-direction: column;margin-top: 1em;
}
</style>

这篇关于【Vue3组件】分享一下自己写的简约风格评论区组件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1087120

相关文章

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

前端下载文件时如何后端返回的文件流一些常见方法

《前端下载文件时如何后端返回的文件流一些常见方法》:本文主要介绍前端下载文件时如何后端返回的文件流一些常见方法,包括使用Blob和URL.createObjectURL创建下载链接,以及处理带有C... 目录1. 使用 Blob 和 URL.createObjectURL 创建下载链接例子:使用 Blob

Vuex Actions多参数传递的解决方案

《VuexActions多参数传递的解决方案》在Vuex中,actions的设计默认只支持单个参数传递,这有时会限制我们的使用场景,下面我将详细介绍几种处理多参数传递的解决方案,从基础到高级,... 目录一、对象封装法(推荐)二、参数解构法三、柯里化函数法四、Payload 工厂函数五、TypeScript

SpringQuartz定时任务核心组件JobDetail与Trigger配置

《SpringQuartz定时任务核心组件JobDetail与Trigger配置》Spring框架与Quartz调度器的集成提供了强大而灵活的定时任务解决方案,本文主要介绍了SpringQuartz定... 目录引言一、Spring Quartz基础架构1.1 核心组件概述1.2 Spring集成优势二、J

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.