【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

相关文章

Vue中组件之间传值的六种方式(完整版)

《Vue中组件之间传值的六种方式(完整版)》组件是vue.js最强大的功能之一,而组件实例的作用域是相互独立的,这就意味着不同组件之间的数据无法相互引用,针对不同的使用场景,如何选择行之有效的通信方式... 目录前言方法一、props/$emit1.父组件向子组件传值2.子组件向父组件传值(通过事件形式)方

css中的 vertical-align与line-height作用详解

《css中的vertical-align与line-height作用详解》:本文主要介绍了CSS中的`vertical-align`和`line-height`属性,包括它们的作用、适用元素、属性值、常见使用场景、常见问题及解决方案,详细内容请阅读本文,希望能对你有所帮助... 目录vertical-ali

浅析CSS 中z - index属性的作用及在什么情况下会失效

《浅析CSS中z-index属性的作用及在什么情况下会失效》z-index属性用于控制元素的堆叠顺序,值越大,元素越显示在上层,它需要元素具有定位属性(如relative、absolute、fi... 目录1. z-index 属性的作用2. z-index 失效的情况2.1 元素没有定位属性2.2 元素处

Python实现html转png的完美方案介绍

《Python实现html转png的完美方案介绍》这篇文章主要为大家详细介绍了如何使用Python实现html转png功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 1.增强稳定性与错误处理建议使用三层异常捕获结构:try: with sync_playwright(

Vue 调用摄像头扫描条码功能实现代码

《Vue调用摄像头扫描条码功能实现代码》本文介绍了如何使用Vue.js和jsQR库来实现调用摄像头并扫描条码的功能,通过安装依赖、获取摄像头视频流、解析条码等步骤,实现了从开始扫描到停止扫描的完整流... 目录实现步骤:代码实现1. 安装依赖2. vue 页面代码功能说明注意事项以下是一个基于 Vue.js

Python解析器安装指南分享(Mac/Windows/Linux)

《Python解析器安装指南分享(Mac/Windows/Linux)》:本文主要介绍Python解析器安装指南(Mac/Windows/Linux),具有很好的参考价值,希望对大家有所帮助,如有... 目NMNkN录1js. 安装包下载1.1 python 下载官网2.核心安装方式3. MACOS 系统安

CSS @media print 使用详解

《CSS@mediaprint使用详解》:本文主要介绍了CSS中的打印媒体查询@mediaprint包括基本语法、常见使用场景和代码示例,如隐藏非必要元素、调整字体和颜色、处理链接的URL显示、分页控制、调整边距和背景等,还提供了测试方法和关键注意事项,并分享了进阶技巧,详细内容请阅读本文,希望能对你有所帮助...

Spring组件初始化扩展点BeanPostProcessor的作用详解

《Spring组件初始化扩展点BeanPostProcessor的作用详解》本文通过实战案例和常见应用场景详细介绍了BeanPostProcessor的使用,并强调了其在Spring扩展中的重要性,感... 目录一、概述二、BeanPostProcessor的作用三、核心方法解析1、postProcessB

kotlin中的行为组件及高级用法

《kotlin中的行为组件及高级用法》Jetpack中的四大行为组件:WorkManager、DataBinding、Coroutines和Lifecycle,分别解决了后台任务调度、数据驱动UI、异... 目录WorkManager工作原理最佳实践Data Binding工作原理进阶技巧Coroutine

Nginx实现前端灰度发布

《Nginx实现前端灰度发布》灰度发布是一种重要的策略,它允许我们在不影响所有用户的情况下,逐步推出新功能或更新,通过灰度发布,我们可以测试新版本的稳定性和性能,下面就来介绍一下前端灰度发布的使用,感... 目录前言一、基于权重的流量分配二、基于 Cookie 的分流三、基于请求头的分流四、基于请求参数的分