下载
npm i clipboard
引入的JS
import Vue from 'vue';
import Clipboard from 'clipboard';
function clipboardSuccess() {
Vue.prototype.$message({
message: '复制成功',
type: 'success',
duration: 1500,
});
}
function clipboardError() {
Vue.prototype.$message({
message: '复制失败',
type: 'error',
});
}
export function handleClipboard(text, event, onSuccess, onError) {
event = event || {};
const clipboard = new Clipboard(event.target, {
text: () => text,
});
clipboard.on('success', () => {
onSuccess ? onSuccess() : clipboardSuccess();
clipboard.off('error');
clipboard.off('success');
clipboard.destroy();
});
clipboard.on('error', () => {
onError ? onError() : clipboardError();
clipboard.off('error');
clipboard.off('success');
clipboard.destroy();
});
clipboard.onClick(event);
}
html:
<img src="XX.png" @click="copy('复制内容')"/>
JS:
import {handleClipboard} from '../clipboard';
// 复制
copy(addr) {
handleClipboard(
addr,
event,
() => {
this.$message.success('成功');
},
() => {
this.$message.error(this.$t('失败'));
}
);
},
还可以参考以下文章
https://blog.csdn.net/guxuehua/article/details/79169190
https://www.jianshu.com/p/091761ff14b1
网友评论