tags: ‘vue’, ‘复制’
categories: ‘vue’, ‘复制’
安装使用
下载依赖
npm install clipboard --save
main.js
在mian.js中引入,当然我们也可以在用到的.vue中引入,因为我不止一个地方用到了复制,所以直接在main.js里面引入了。
import clipboard from 'clipboard'; // 引入clipboard,这是复制的插件Vue.prototype.clipboard = clipboard; //注册到vue原型上
.vue中调用
更多操作查看官方文档
<template><div><div class="content"><p>aaaa</p><p>bbbb</p><p>cccc</p><p>dddd</p></div><button class="Btn">复制</button></div></template><script>let _this = '';export default{methods: {initBtnCopy(nameBtn,nameContent){_this.myClipboard ? _this.myClipboard.destroy() : '';this.myClipboard = new Clipboard(nameBtn, {target: function() {return document.querySelector(nameContent);}});this.myClipboard.on('success', function(e) {_this.$Message.success("复制成功!");// 先销毁之前的,再重新定义_this.myClipboard.destroy();initBtnCopye.clearSelection();});this.myClipboard.on('error', function(e) {_this.$Message.info('请按 Ctrl+C 复制文字');});}},beforeCreate : function (){_this = this;},mounted(){this.initBtnCopy(".Btn", ".content");}}</script>
