手搓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. 渲染模板…

Go实现mongodb增删改查的工具类

文章目录 1、驱动下载2、实现代码2.1 Mongodb工具类代码2.2 使用例子2.3 运行效果 1、驱动下载 mongodb官方go介绍 使用例子https://www.mongodb.com/docs/drivers/go/current/fundamentals/connection/#connection-example 快速入门https://www.mongodb.com/docs/drivers/go/…

git和github学习

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

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

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

第21题-巨大的数:给你n个数 ai,求这n个数相乘之后的积的个位数字是多少,0 < n,ai <= 100...

问题 : 巨大的数 时间限制: 1Sec 内存限制: 128MB 题目描述 给你n个数 ai&#xff0c;求这n个数相乘之后的积的个位数字是多少&#xff0c;0 < n,ai < 100 输入 共两行&#xff0c;第一行为n的值&#xff0c;表示有多少个数&#xff0c;第二行为由空格隔开的n个数 …

源码解析Flink源节点数据读取是如何与checkpoint串行执行

文章目录 源码解析Flink源节点数据读取是如何与checkpoint串行执行Checkpoint阶段StreamTask类变量actionExecutor的实现和初始化小结 数据读取阶段小结 总结 源码解析Flink源节点数据读取是如何与checkpoint串行执行 Flink版本&#xff1a;1.13.6 前置知识&#xff1a;源节点…

三天从零快速入门React

前言 React 官网文档比较完善&#xff0c;本文更注重结合实际项目中常见的问题&#xff0c;来介绍 React 的用法 Fun Facts ReactVueAngularNPM weekly downloads &#xff08;由于 cnpm 无法查看包&#xff0c;数据不全&#xff09;12,635,9662,662,666823,653Dependents59…

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…

什么是 API 安全?学习如何防止攻击和保护数据

随着 API 技术的普及&#xff0c;API 安全成为了一个越来越重要的问题。本文将介绍什么是 API 安全&#xff0c;以及目前 API 面临的安全问题和相应的解决方案。 什么是 API 安全 API 安全是指保护 API 免受恶意攻击和滥用的安全措施。API 安全通常包括以下几个方面&#xff1…

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中返回网络地址,在错…

高质量api接口对接及Python示例代码

当我们需要将不同系统或服务进行对接时&#xff0c;接口对接是一种常见的解决方案。我将介绍如何使用Python进行接口对接&#xff0c;并提供示例代码。 首先&#xff0c;我们需要导入Python的requests库&#xff0c;它是一个常用的HTTP请求库&#xff0c;可以方便地发送HTTP请求…

第九次作业

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

提升城市管理效率,软件机器人助力自动化处理投诉、建议、举报

在现代城市管理中&#xff0c;市民的投诉、建议和举报等事项是不可忽视的重要环节。然而&#xff0c;传统的处理方式往往需要大量的人力和时间&#xff0c;效率较低。为了提升城市管理部门的服务质量和效率&#xff0c;引入软件机器人成为一种可行的选择。 博为小帮软件机器人可…

Python京东商品详情页数据采集方法,京东 API 接口介绍

京东详情接口 API 是开放平台提供的一种 API 接口&#xff0c;它可以帮助开发者获取商品的详细信息&#xff0c;包括商品的标题、描述、图片等信息。在电商平台的开发中&#xff0c;详情接口 API 是非常常用的 API&#xff0c;因此本文将详细介绍详情接口 API 的使用。 一、京…

运算符重载---1

运算符重载---1 //运算符重载//内置类型可以直接使用运算符运算&#xff0c;编译器知道要如何运算。 //但自定义类型无法直接使用运算符&#xff0c;因为编译器不知道要如何运算。如果想支持&#xff0c;自己实现运算符重载即可。// C为了增强 代码的可读性 引入了运算符重载&a…