vue在移动端实现复制数值到剪贴版
2019-11-04 本文已影响0人
哒哒哒哒da
实现按键指定内容复制到设备的剪贴版
引入clipboard.js
npm install clipboard --save
封装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
<span class="coinHttp">
<span>{{name}}</span>
<img src="..//XXXX.png" @click="copy" />
</span>
JS
import {handleClipboard} from '../js/clipboard';
methods: {
copy() {
handleClipboard(this.express.number, event, () => {
alert('单号已经复制在剪贴版')
}, () => {
alert('单号复制失败!')
})
},
}