当前位置:网站首页>基于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>
效果
样式可以根据自己的需求修改,可以更换图标

文章如有错误,恳请大家提出问题,本人不胜感激 。 不懂的地方可以评论,我都会 一 一回复
文章对大家有帮助的话,希望大家能动手点赞鼓励,大家未来一起努力 长路漫漫,道阻且长
边栏推荐
- (1), the sequential storage structure of linear table chain storage structure
- 湖北移动中兴B860AV2.1_S905L_线刷固件包
- 乐享购(分享购)的模式:优势、亮点、收益
- 太一集团宣布全资收购火币旗下社交产品火信
- 【 Gazebo introductory tutorial] speak the second model library into robot modeling and visualization (editor) model
- 【论文阅读】Decision Transformer: Reinforcement Learning via Sequence Modeling
- 安装win11提示开启安全模式如何解决
- 重新审视分布式系统:永远不会有完美的一致性方案……
- 学习探索-给字体设置前景色
- 8月5日,麒麟信安邀您相约鲲鹏开发者创享日·长沙站!
猜你喜欢

全世界国家和地区国家顶级域名对照表

提高图片清晰度的快速方法?

浙江数码代工M301H 免拆通刷_卡刷固件包(语音OK)

安装win11提示开启安全模式如何解决

【LeetCode每日一题】——540.有序数组中的单一元素

Boost库学习笔记(一)安装与配置

小满nestjs(第一章 介绍nestjs)

知乎高赞:拼多多和国家电网,选哪个?

移动魔百盒CM211-1_YS代工_S905L3B_RTL8822C_线刷固件包
![【 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
随机推荐
To eliminate asynchronous callbacks, it has to be async-await
Mobile magic box CM211-1_YS foundry _S905L3B_RTL8822C_wire brush firmware package
SRM Supplier Collaborative Management System Function Introduction
机器学习(十六):主成成分分析(PCA)
浙江数码代工M301H 免拆通刷_卡刷固件包(语音OK)
LeetCode 0168. Excel表列名称
RTL8762DK 远端设备配对
从-99打造Sentinel高可用集群限流中间件
8月5日,麒麟信安邀您相约鲲鹏开发者创享日·长沙站!
Taurus.MVC WebAPI 入门开发教程2:添加控制器输出Hello World。
Selenium Webdriver驱动自管理
适配器模式
ES中同时使用should和must导致只有must生效解决方案
Hubei Mobile ZTE B860AV2.1_S905L_ flash firmware package
xgboost模块param中的一些错误
机器学习(十):朴素贝叶斯
el-date-picker 设置时间范围
R语言使用cov函数计算矩阵或者dataframe数据变量之间的协方差、cor函数计算相关性、cor函数通过method参数指定相关性、相关性计算方法Pearson,Spearman, Kendall
shell脚本详解 --------循环语句之for循环
不需要服务器,教你仅用30行代码搞定实时健康码识别