700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > vue clipboard复制文本到剪切板

vue clipboard复制文本到剪切板

时间:2024-02-04 11:27:43

相关推荐

vue clipboard复制文本到剪切板

import Clipboard from 'clipboard'// 复制fn(){let msg = ‘需要复制的文本’let clipboard = new Clipboard('.tag', {text: function () {return msg}})clipboard.on('success', e => {this.$message({message: '复制成功', showClose: true, type: 'success'})// 释放内存clipboard.destroy()})clipboard.on('error', e => {this.$message({message: '复制失败,', showClose: true, type: 'error'})clipboard.destroy()})// 原生方法const save = function (e) {e.clipboardData.setData("text/plain", msg);e.preventDefault(); // 阻止默认行为};// once 表示 listener 在添加之后最多只调用一次。如果是 true, listener 会在其被调用之后自动移除const once = {once: true,};document.addEventListener("copy", save,once); // 添加一个copy事件,当触发时执行一次,执行完删除document.execCommand("copy"); // 执行copy方法this.$message({message: "复制成功", type: "success" });}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。