第一步: cnpm install -S clipboard
第二步:
<template>
<div id="app">
<div class="box">点我复制</div>
</div>
</template>
<script>
import Clipboard from 'clipboard';
export default {
mounted(){
let box = document.querySelector(".box")
box.onclick = function (e) {
if(!this.copyPhone){ // 被复制到剪贴板上的内容
this.setAttribute('data-clipboard-text', "13932121321");
this.copyPhone = new Clipboard(box);
this.copyPhone.on('success', e => {
console.log('电话复制成功');
});
this.copyPhone.on('error', e => {
console.log('不支持复制');
this.copyPhone.destroy();
})
box.click();
}
}
},
代码完整截图:
效果: