问题: 初次进入页面点击复制只会出现一次提示,页面切换只会再次点击会重复N次提示
解决方法
- mounted 的时候绑定实例,destroyed 的时候销毁实例
methods: {
copy () {
if (JSON.stringify(this.clipboard) !== '{}') {
this.clipboard.destroy()
}
let _this = this
this.clipboard = new Clipboard('.copyText')
this.clipboard.on('success', function (e) {
console.log('success')
_this.$Message.success('UID复制成功')
e.clearSelection()
})
this.clipboard.on('error', function (e) {
_this.$Message.success('复制失败')
console.error(e)
})
},
mounted: function () {
this.copy()
},
destroyed: function () {
this.clipboard.destroy()
}
分析
因为在mounted
的时候new
一个实例,再次进入页面时候又会new
一个实例,因此会出现多次提示。确保当前页面只有一个clipboard
实例就可以了