这里有一个复制文本的dom
<template>
<div>
<span id="bar">需要复制的文本</span>
<button class="btn" data-clipboard-target="#bar">点击复制文本</button>
</div>
</template>
<script>
import ClipboardJS from 'clipboard'
export default {
mounted () {
this.clip = new ClipboardJS('.btn')
this.clip.on('success', () => {
alert('复制成功')
})
this.clip.on('error', (e) => {
console.error(e)
alert('复制失败,请重试')
})
this.$once('hook:beforeDestroy', () => {
this.clip && this.clip.destroy()
})
}
}
</script>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…