clipboard 官网npm
能够经过npm工具安装浏览器
npm install clipboard --save
复制代码
若是你不打算进行包管理,能够直接下载 zip文件安全
选择一个触发复制事件的DOM对象;.icon-copy工具
<el-button
class="btn confirm copy icon-copy"
ref="copy"
data-clipboard-action="copy"
data-clipboard-target="#inviteInfo"
>复制连接</el-button>
复制代码
初始化实例对象:this
toInitClipboard() {
this.copyBtn = new this.clipboard(".icon-copy");
},
复制代码
事件:实例对象监听copy事件,并做出对应的操做:spa
clipboard.on("success", function() {
_this.$message({
message: tips.copySuccessTip,
type: "success",
duration: "3000"
});
//当copy成功或者失败的时候,销毁当前监听对象;
clipboard.destroy();
});
clipboard.on("error", function() {
_this.$message({
message: tips.copyFailedTip,
type: "error",
duration: "3000"
});
//当copy成功或者失败的时候,销毁当前监听对象;
clipboard.destroy();
});
复制代码
单页应用:能够更加精确地管理 DOM 的生命周期。你能够清理事件以及建立的对象。code
这个库依赖 Selection 和 execCommand APIs. 前者兼容 全部的浏览器 后者只兼容如下浏览器版本。cdn
好消息是,若是你须要支持旧浏览器,clipboard.js 能够优雅降级。你所要作的就是在 success 事件触发时提示用户“已复制!”,error 事件触发时提示用户“按 Ctrl+C 复制文字”(此时用户要复制的文字已经选择了)。对象
你也能够经过运行 Clipboard.isSupported() 来检查浏览器是否支持 clipboard.js,若是不支持,你能够隐藏复制/剪切按钮。blog
<template>
<div>
<!-- 邀请 -->
<div style="margin-left: 15px;cursor: pointer">
<div class="inviteBtnContainer" @click="toOpenInviteDialog">
<span class="inviteBtnInfo">邀请</span>
</div>
</div>
<div>
<el-dialog
:visible.sync="inviteDialogVisible"
width="530px"
z-index="6000"
@close="handleCreateInviteClose"
:close-on-click-modal="false"
>
<div style="width:100%;">
<img style="width:inherit" src="../../assets/img/invite.png" alt>
</div>
<div id="inviteTitleInfo" style="margin-top:29px;">
<div style="text-align: center">
<span class="inviteTitle">邀请其余企业</span>
</div>
<div style="text-align: center">
<span class="inviteTip">经过连接邀请的新的用户接受邀约后,注册经过后便可登陆企业平台。</span>
</div>
</div>
<div id="inviteInfo" class="inviteInfo">
<div class="inviteContent">
<span>【{{tenantName}}】</span>
<span>企业管理员{{nickName}}</span>
<span>给你发来连接 :</span>
<div style="margin: 3px 0;">
<a>{{encodeUrl}}</a>
</div>
<div>
<span>点击进入xxx+平台一块儿协做工做吧!连接24小时内有效,为确保企业信息安全,切勿随意传播该条信息,客户服务热线021-606xxxxx【xxx+】</span>
</div>
</div>
</div>
<span slot="footer" class="dialog-footer footer-display center">
<el-button
class="btn confirm copy icon-copy"
ref="copy"
data-clipboard-action="copy"
data-clipboard-target="#inviteInfo"
>复制连接</el-button>
<el-button class="btn cancel" @click="inviteDialogVisible = false">取消</el-button>
</span>
</el-dialog>
</div>
</div>
</template>
exportdefault {
data(){
return {
copyBtn:null,
}
},
created() {
this.init();
},
methods: {
init() {
this.toInitClipboard();
},
toInitClipboard() {
this.copyBtn = new this.clipboard(".icon-copy");
},
handleCopyInvitationCode() {
let _this = this;
let clipboard = _this.copyBtn;
clipboard.on("success", function() {
_this.$message({
message: tips.copySuccessTip,
type: "success",
duration: "3000"
});
//当copy成功或者失败的时候,销毁当前监听对象;
clipboard.destroy();
});
clipboard.on("error", function() {
_this.$message({
message: tips.copyFailedTip,
type: "error",
duration: "3000"
});
//当copy成功或者失败的时候,销毁当前监听对象;
clipboard.destroy();
});
},
}
}
复制代码