当前位置:网站首页>基于clipboard.js对复制组件的封装
基于clipboard.js对复制组件的封装
2022-08-04 17:02:00 【潮汐未见潮落】
前言
在日常开发中,有时可能想实现一键复制,我们可以选择手写复制方法,也可以选择引入 clipboard.js 库帮助快速实现功能
本次封装组件使用 element-ui 的部分组件,有 icon 、message 等
安装clipboard
npm install clipboard --save子组件
<!--
* @description: 封装点击复制的组件
* @since: v1.0
!-->
<template>
<i :class="`${icon} icon-cursor`"
title="点击复制"
@click="handleCopy($event, text)" />
</template>
<script>
// 引入 clipboard.js
import Clipboard from 'clipboard';
export default {
props: {
// 接收复制的内容
text: {
type: [String, Number],
default: null,
},
// 默认是复制 icon,可自定义 icon
icon: {
type: [String],
default: 'el-icon-copy-document',
},
// 自定义成功提示
message: {
type: [String, Number],
default: null,
},
},
methods: {
handleCopy (e, _text, message) {
const clipboard = new Clipboard(e.target, { text: () => _text });
// const messageText = message || `复制成功:${_text}`;
const messageText = message || '复制成功';
// 复制成功
clipboard.on('success', () => {
this.$message({
type: 'success',
message: messageText
});
clipboard.off('error');
clipboard.off('success');
clipboard.destroy();
});
// 复制失败
clipboard.on('error', () => {
this.$message({
type: 'warning',
message: '复制失败,请手动复制'
});
clipboard.off('error');
clipboard.off('success');
clipboard.destroy();
});
clipboard.onClick(e);
},
},
};
</script>
<style lang="scss" scoped>
.icon-cursor {
cursor: pointer;
}
</style>
父组件
<template>
<div>
<span>{
{ value }}</span>
<CopyIcon :text="value" />
</div>
</template>
<script>
import CopyIcon from '@/components/CopyIcon.vue'
export default {
components: {
CopyIcon,
},
data () {
return {
value: '这里测试一下复制组件',
};
},
};
</script>
效果
样式可以根据自己的需求修改,可以更换图标

文章如有错误,恳请大家提出问题,本人不胜感激 。 不懂的地方可以评论,我都会 一 一回复
文章对大家有帮助的话,希望大家能动手点赞鼓励,大家未来一起努力 长路漫漫,道阻且长
边栏推荐
- 御神楽的学习记录之基于FPGA的AHT10温湿度数据采集
- JSP 标准标签库(JSTL)[通俗易懂]
- 美容院管理系统有哪些促销方式?
- R语言使用cov函数计算矩阵或者dataframe数据变量之间的协方差、cor函数计算相关性、cor函数通过method参数指定相关性、相关性计算方法Pearson,Spearman, Kendall
- 学习探索-网站中引入百度统计
- 谷歌开发者社区推荐:《Jetpack Compose 从入门到实战》新书上架,带你踏上 Compose 开发之旅~
- Heilongjiang Mobile New Magic Hundred Box M411A_2+8_S905L3A_wire brush firmware package
- 为什么买域名必须实名认证?这样做什么原因?
- 移动平台助力推进智慧型科研院所信息化建设
- 重新审视分布式系统:永远不会有完美的一致性方案……
猜你喜欢
![【 Gazebo introductory tutorial] speak the second model library into robot modeling and visualization (editor) model](/img/db/44a1ac5338879c9e6edd933c28c0af.png)
【 Gazebo introductory tutorial] speak the second model library into robot modeling and visualization (editor) model

移动魔百盒CM211-1_YS代工_S905L3B_RTL8822C_线刷固件包

win11如何退出安全模式

Hubei Telecom Tianyi TY1608_S905L3B_MT7668_ card brush firmware package

移动百事通BesTV_R3300-L_S905L_8189_线刷固件包

Catering Supply Chain Management System

15 days to upgrade to fight monsters and become a virtual fashion creator

全球电子产品需求萎靡:三星越南工厂大幅压缩产能,减少工人工作日

LeetCode 每日一题——1403. 非递增顺序的最小子序列

Mobile Hisense IP102H_905L3-B_wire brush firmware package
随机推荐
To eliminate asynchronous callbacks, it has to be async-await
(1), the sequential storage structure of linear table chain storage structure
为什么买域名必须实名认证?这样做什么原因?
Mobile Hisense IP102H_905L3-B_wire brush firmware package
GraphQL 入门与实践
WPF 光标初始化的时候 temp 文件夹满了无法创建
Cesium快速上手0-Cesium安装与基本介绍
罗振宇折戟创业板/ B站回应HR称用户是Loser/ 腾讯罗技年内合推云游戏掌机...今日更多新鲜事在此...
拼多多详情API接口深度解读
餐饮供应链管理系统
8月5日,麒麟信安邀您相约鲲鹏开发者创享日·长沙站!
机器学习入门到大神专栏总览
湖北移动HG680-LV_S905L3B_线刷固件包
WEB 渗透之越权
安装win11提示开启安全模式如何解决
R语言使用yardstick包的gain_curve函数评估多分类(Multiclass)模型的性能、查看模型在多分类每个分类上的增益(gain)曲线(gain curve)
jMeter Transaction Controller 学习笔记
JSP的Web监听器(Listener)
移动中兴ZXV10 B860AV2.1-A_S905L2_MT7668_线刷固件包
机器学习(十四):K均值聚类(kmeans)