手搓vue3组件_1.封装一个button

我的icepro参考地址,内有参考代码,有条件的割割点点star

实现要求:

  • 基于vue3
  • 支持通过colors(更改颜色)
  • 支持点击事件
  • …支持其他的自定义样式(例如圆角,size等等)

最基础的第一步:

父组件引入并使用:

<template><div class="buttonLim">我的按钮:<ice-button>primary</ice-button></div>
</template>
<script setup>
import IceButton from '../../components/other/ice-button.vue'
</script>
<style scoped lang="less">
</style>

子组件中使用slot去展示:

<template><div class="ice-button"><slot></slot></div>
</template>
<script setup>
</script>
<style scoped lang="less">
</style>

run:

在这里插入图片描述

那么,把它的样式改的好看一些:

父组件:

<template><div class="buttonLim">我的按钮:<ice-button>primary</ice-button></div>
</template><script setup>
import IceButton from '../../components/other/ice-button.vue'</script><style scoped lang="less">
.buttonLim {display: flex;justify-content: center;flex-direction: column;align-items: center;
}
</style>

子组件:

<template><div class="ice-button"><slot></slot></div>
</template><script setup></script><style scoped lang="less">
.ice-button {border-radius: .3rem;border: rgba(0, 0, 0, .7) 1px solid;background: rgba(0, 0, 0, .2);width: fit-content;padding: .2rem .4rem;margin: .1rem .2rem;user-select: none;
}</style>

在这里插入图片描述
当然,此时他的颜色并不够好看,那么如果想通过props向子组件自定义颜色:
子组件:

<template><div class="ice-button"><slot></slot></div>
</template><script setup>
const props = defineProps({color: {type: String,default: ''}
})
</script>

这样你传过来了,但是想怎么用呢,

这里要求颜色有未hover时的颜色和hover时的颜色,hover时的颜色自动计算出来

而此时可以考虑使用到css的变量了,像是:
子组件:

<template><div class="ice-button":class="[color?'hoverColor':'defaultColor']":style="{ '--color': color,'--hover-color': hoverColor(color) }"><slot></slot></div>
</template><script setup>
const props = defineProps({color: {type: String,default: ''}
})
const hoverColor = (rgb) => {return rgb.replaceAll(')', ',.5)')
}
</script><style scoped lang="less">
.ice-button {border-radius: .3rem;width: fit-content;padding: .2rem .4rem;margin: .1rem .2rem;user-select: none;transition-duration: .3s;
}.defaultColor {border: rgba(0, 0, 0, .7) 1px solid;background: rgba(0, 0, 0, .2);
}.hoverColor {color: var(--color);border: var(--color) 1px solid;&:hover {color: var(--hover-color);border: var(--hover-color) 1px solid;}
}
</style>

父组件的调用:

<template><div class="buttonLim">我的按钮:<ice-button color="rgb(251, 139, 5)">primary</ice-button><ice-button color="rgb(234, 137, 88)">primary</ice-button></div>
</template><script setup>
import IceButton from '../../components/other/ice-button.vue'</script><style scoped lang="less">
.buttonLim {display: flex;justify-content: center;flex-direction: column;align-items: center;
}
</style>

run:
在这里插入图片描述

解释一下:

子组件中,如果传入了color的值,那么子组件的类名hoverColor生效,反之defaultColor生效,这里是给class传入了一个数组,如果你查看elementui的源码,会发现他们也是这样实现组件的type的切换,用过了才知道这个技巧是如此好用

还有,这里只是传入了一个rgb的值,然后在子组件中自动计算出来另一个颜色值(直接改为rgba,opacity为0.5)

支持点击事件

如果你直接使用下面的方式来绑定:
父组件:

<template><div class="buttonLim">我的按钮:<ice-button color="rgb(251, 139, 5)">primary</ice-button><ice-button color="rgb(234, 137, 88)">primary</ice-button><ice-button @click="clickTrigger" color="rgb(242, 72, 27)" ref="btn">click</ice-button></div>
</template><script setup>
import IceButton from '../../components/other/ice-button.vue'
import { ref } from 'vue'const btn = ref()
const clickTrigger = async () => {console.log('clickTrigger--->')const str = '我即将要赋值的文字'if (await copyText(str)) {console.log('success')} else {console.log('error')}
}const copyText = function (str) {return navigator.clipboard.writeText(str).then(() => {return true}).catch(() => {return false})
}</script><style scoped lang="less">
.buttonLim {display: flex;justify-content: center;flex-direction: column;align-items: center;
}
</style>

子组件:

<template><div class="ice-button":class="[color?'hoverColor':'defaultColor']":style="{ '--color': color,'--hover-color': hoverColor(color) }"><slot></slot></div>
</template><script setup>
const props = defineProps({color: {type: String,default: ''}
})
const hoverColor = (rgb) => {return rgb.replaceAll(')', ',.5)')
}
</script><style scoped lang="less">
.ice-button {border-radius: .3rem;width: fit-content;padding: .2rem .4rem;margin: .1rem .2rem;user-select: none;transition-duration: .3s;
}.defaultColor {border: rgba(0, 0, 0, .7) 1px solid;background: rgba(0, 0, 0, .2);
}.hoverColor {color: var(--color);border: var(--color) 1px solid;&:hover {color: var(--hover-color);border: var(--hover-color) 1px solid;}
}
</style>

这样没问题可以,但是有时会报错,click不是原生事件,这里我没有复现,淡然,你也可以在复习bug的时候想起这篇文章

这里的逻辑是点击左侧的item,赋值文字,但是这里的子组件没有定义click的处理事件,上面的button也是,可能会报这种错,

  • 如何解决:

在子组件中定义click事件:
子组件:

<template><div class="ice-button"@click="clickCallBack":class="[color?'hoverColor':'defaultColor']":style="{ '--color': color,'--hover-color': hoverColor(color) }"><slot></slot></div>
</template><script setup>
const props = defineProps({color: {type: String,default: ''}
})
const hoverColor = (rgb) => {return rgb.replaceAll(')', ',.5)')
}const emit = defineEmits(['click'])
const clickCallBack = (evt) => {emit('click', evt)
}
</script><style scoped lang="less">
.ice-button {border-radius: .3rem;width: fit-content;padding: .2rem .4rem;margin: .1rem .2rem;user-select: none;transition-duration: .3s;
}.defaultColor {border: rgba(0, 0, 0, .7) 1px solid;background: rgba(0, 0, 0, .2);
}.hoverColor {color: var(--color);border: var(--color) 1px solid;&:hover {color: var(--hover-color);border: var(--hover-color) 1px solid;}
}
</style>

这里的clickCallBack接收并emit一下click事件
emit函数会触发父组件绑定的click事件。当用户点击按钮时,父组件会接收到这个事件,并执行相应的操作。

自定义圆角

这里其实还是使用props来自定义圆角,例如我实现下面几个(round和block)按钮:
在这里插入图片描述
父组件的调用:

    自定义圆角:<ice-button round>round</ice-button><ice-button block>block</ice-button>

子组件:

<template><div class="ice-button"@click="clickCallBack":class="[color?'hoverColor':'defaultColor',round?'round':'',block?'block':'']":style="{ '--color': color,'--hover-color': hoverColor(color) }"><slot></slot></div>
</template><script setup>
const props = defineProps({color: {type: String,default: ''},round: {type: Boolean,default: false},block: {type: Boolean,default: false}
})
const hoverColor = (rgb) => {return rgb.replaceAll(')', ',.5)')
}const emit = defineEmits(['click'])
const clickCallBack = (evt) => {emit('click', evt)
}
</script><style scoped lang="less">
.ice-button {border-radius: .3rem;width: fit-content;padding: .2rem .4rem;margin: .1rem .2rem;user-select: none;transition-duration: .3s;
}.defaultColor {border: rgba(0, 0, 0, .7) 1px solid;color: rgba(0, 0, 0, .7);transition-duration: .3s;&:hover {color: rgba(0, 0, 0, .4);border: rgba(0, 0, 0, .4) 1px solid;}
}.hoverColor {color: var(--color);border: var(--color) 1px solid;&:hover {color: var(--hover-color);border: var(--hover-color) 1px solid;}
}.round {border-radius: 2rem;
}.block {border-radius: 0;
}
</style>

当然,也可以混合使用:
在这里插入图片描述

    <ice-button block color="rgb(242, 72, 27)">混合</ice-button>

以上说的功能能都实现了

注意这里的代码还有很多没有优化,颜色获取,其他自定义type之类的都没有处理,关于更多的细节优化,详见icepro

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/28845.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java课题笔记~ 关于错误与异常

非检查异常(unckecked exception)&#xff1a;Error 和 RuntimeException 以及他们的子类。javac在编译时&#xff0c;不会提示和发现这样的异常&#xff0c;不要求程序员必须处理这些异常。在运行阶段&#xff0c;倘若发生Error则虚拟机几乎崩溃&#xff0c;倘若发生RuntimeEx…

Django快速入门

文章目录 一、安装1.创建虚拟环境&#xff08;virtualenv和virtualenvwrapper&#xff09;2. 安装django 二、改解释器三、创建一个Django项目四、项目目录项目同名文件夹/settings.py 五、测试服务器启动六、数据迁移七、创建应用八、基本视图1. 返回响应 response2. 渲染模板…

git和github学习

一、什么是git和github? 二、学会使用github desktop应用程序 初始使用&#xff1a; 一开始我们是新账户&#xff0c;里面是没有仓库的&#xff0c;需要手动创建一个仓库。此时&#xff0c;这个仓库是创建在本地仓库里面&#xff0c;需要用到push命令&#xff08;就是那个pub…

Vantage透明屏的工作原理是什么?应用、展示、显示

Vantage透明屏是一种新型的显示技术&#xff0c;它能够将图像和视频直接投影到透明的屏幕上&#xff0c;使得观众可以同时看到屏幕上的内容和背后的实物。 这种技术在广告、展览、零售和娱乐等领域有着广泛的应用前景。 Vantage透明屏的工作原理是利用透明的显示面板和背后的…

AI深度学习部署全记录

AI部署流程&#xff0c;以PyTorch为例&#xff1a; 1.Torch.Model->ONNX->ONNXSIM->TensortRT->落地 2.Torch.Model->Pt->ONNX->ONNXRunTime->落地 3.Torch.Model->Pt->Libtorch->落地 4.Torch.Model->PNNX->TensorRT->落地 5.…

sql刷题

文章目录 section A1 各部门工资最高的员工&#xff08;难度&#xff1a;中等&#xff09;2 换座位&#xff08;难度&#xff1a;中等&#xff09;3 分数排名&#xff08;难度&#xff1a;中等&#xff09;4 连续出现的数字&#xff08;难度&#xff1a;中等&#xff09;5 树节…

GD32F103VE串口中断发送和接收

GD32F103VE串口中断发送和接收&#xff0c;本程序基于RS485完成测试&#xff0c;实现将收到的数据&#xff0c;再发送出去。 #include "USART1_Interrupt.h" #include "stdio.h" //getchar(),putchar(),scanf(),printf(),puts(),gets(),sprintf() #inclu…

Zabbix监控华为交换机DHCP接口地址池

一、背景 最近工作中遇到一个因为DHCP地址池满载、导致用户无法获取到IP地址的故障&#xff0c;所以在想通过zabbix 监控DHCP地址池的状态、当DHCP 地址池数量小于某个值时触发zabbix告警。 网上找了一下没有相关的文档、和对应的OID值、于是用Python 脚本的方式实现 二、实现效…

电视盒子哪个牌子好?拆机达人揭晓电视盒子品牌排行榜

老赵每天会对各种类型的数码产品进行拆机&#xff0c;对硬件、品控这块非常熟悉&#xff0c;近期很多朋友问我电视盒子哪个牌子好&#xff0c;我整理了目前市面上硬件、软件都表现不错的电视盒子品牌排行榜&#xff0c;看看目前最值得入手的电视盒子都有哪些。 第一&#xff1a…

无涯教程-Perl - getnetent函数

描述 此函数从/etc/networks文件获取下一个条目,返回-($name,$aliases,$addrtype,$net) 如果/etc/networks文件为空,则它将不返回任何内容,并且调用将失败。 语法 以下是此函数的简单语法- getnetent返回值 此函数在错误时返回undef,否则在标量context中返回网络地址,在错…

第九次作业

1. SSL工作过程是什么&#xff1f; 当客户端向一个 https 网站发起请求时&#xff0c;服务器会将 SSL 证书发送给客户端进行校验&#xff0c;SSL 证书中包含一个公钥。校验成功后&#xff0c;客户端会生成一个随机串&#xff0c;并使用受访网站的 SSL 证书公钥进行加密&#xf…

手搓单链表

文章目录 前言一、链表和顺序表的区别二、什么是单链表单链表分类单链表的结构 三、带头不循环单链表1.单链表的结构体2.带头不循环单链表的初始化和销毁3.带头不循环单链表的头插&#xff0c;尾插和打印4.带头不循环单链表的头删和尾删5.带头不循环单链表的查找&#xff0c;指…

进程通信常见方式

目录 通信通信概述 通信的主要方式 进程同步机制--低级进程通信 高级通信工具 共享存储器系统(Shared-Memory System&#xff09; 管道(pipe)通信系统 客户机-服务器系统(Client-Server system)---套接字&#xff08;Socket&#xff09; 客户机-服务器系统(Client-Serv…

国内什么牌子的ipad手写笔好用?适合绘画电容笔推荐

对于那些想要用ipad来学习的人来说&#xff0c;苹果Pencil是必不可少的。但是&#xff0c;Apple Pencil的价格真的太贵了&#xff0c;以至于很多人都买不起。所以&#xff0c;最好的办法就是选用一支平替的电容笔。本人从前几年就开始使用iPad&#xff0c;同时本身也是一位数码…

冠达管理:创新药概念强势拉升,康希诺大涨超15%

立异药概念9日盘中强势拉升&#xff0c;到发稿&#xff0c;昊帆生物“20cm”涨停&#xff0c;康希诺大涨超15%&#xff0c;翰宇药业涨近13%&#xff0c;德展健康涨停&#xff0c;泰格医药、药石科技涨超7%。 康希诺昨日晚间公告&#xff0c;8月7日&#xff0c;公司与 AstraZene…

【三维重建】【深度学习】windows10下instant-nsr-pl代码Pytorch实现

【三维重建】【深度学习】windows10下instant-nsr-pl代码Pytorch实现 提示:基于 Instant-NGP 和 Pytorch-Lightning 框架的神经表面重建 文章目录 【三维重建】【深度学习】windows10下instant-nsr-pl代码Pytorch实现前言instant-nsr-pl模型运行下载源码并安装环境训练instant-…

那些没人教你的Jmeter 循环断言,百度不到的,收藏一下吧

前言 对于使用jmeter工具完成接口测试的测试工程师而言。在工作中&#xff0c;或者在面试中&#xff0c;都会遇到一个问题。 CSV文档做了一大笔测试数据后&#xff0c;怎么去校验这个结果呢&#xff1f; 现在大部分测试工程师可能都是通过人工的方法去查看结果&#xff0c;十几…

java中javamail发送带附件的邮件实现方法

java中javamail发送带附件的邮件实现方法 本文实例讲述了java中javamail发送带附件的邮件实现方法。分享给大家供大家参考。具体分析如下&#xff1a; JavaMail&#xff0c;顾名思义&#xff0c;提供给开发者处理电子邮件相关的编程接口。它是Sun发布的用来处理email的API。它…

网络安全 Day27-运维安全项目-堡垒机部署

运维安全项目-堡垒机部署 1. 运维安全项目-架构概述2. 运维安全项目之堡垒机2.1 堡垒机概述2.2 堡垒机选型2.3 环境准备2.4 部署Teleport堡垒机2.4.1 下载与部署2.4.2 启动2.4.3 浏览器访问teleport2.4.4 进行配置2.4.5 安装teleport客户端 2.5 teleport连接服务器 1. 运维安全…

Elasticsearch官方测试数据导入

一、数据准备 百度网盘链接 链接&#xff1a;https://pan.baidu.com/s/1rPZBvH-J0367yQDg9qHiwQ?pwd7n5n 提取码&#xff1a;7n5n文档格式 {"index":{"_id":"1"}} {"account_number":1,"balance":39225,"firstnam…