vue指令复制

cnpm install clipboard --save-dev
import clipboard from 'clipboard';

Vue.prototype.clipboard = clipboard;

 

        <el-button @click="copy(categoryCode)" id="getCategoryCode">复制</el-button>

        copy (data) {
                let clipboard = new this.clipboard('#getCategoryCode', {
                text: function () {
                    return data
                }
            })
            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()
            })
        },

上面是直接实用的方式

下面是我写的指令方式复制

cnpm install clipboard --save-dev  // 安装依赖
import clipboard from 'clipboard'; // main.js 引入
Vue.directive('copy', {       // 自定义指令copy
    bind(el, bingind, vnode) {
        let copy = new clipboard(el, {
            text: function () {
                return JSON.stringify(bingind.value)
            }
        });
        copy.on('success', e => {
            alerts('复制成功', 'success') // 自己写的弹框
        })
        copy.on('error', e => {
            alerts('复制失败,', 'error')  // 自己写的弹框
        })
    }
});

<el-button v-copy="params">复制</el-button> // 页面中使用 params是要复制的内容

或者

import clipboard from 'clipboard';
Vue.directive('copy', {
    bind(el, bingind, vnode) {
        const copy = new clipboard(el);
        el.dataset.clipboardText = JSON.stringify(bingind.value)
        copy.on('success', e => {
            alerts('复制成功', 'success') // 自己写的弹框
        });
        copy.on('error', e => {
            alerts('复制失败,', 'error')  // 自己写的弹框
        });
    },
    update(el, bingind) {
        el.dataset.clipboardText = JSON.stringify(bingind.value)
    }
});

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值