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" });}
700字范文,内容丰富有趣,生活中的好帮手!