直接上代码
class ClipboardTool {
copy(text: any, toastText?: any) {
const input = document.createElement('input');
input.setAttribute('readonly', 'readonly');
input.setAttribute('value', text || '');
document.body.appendChild(input);
input.setSelectionRange(0, text.length);
if (document.execCommand('copy')) {
input.select();
document.execCommand('copy');
ToastTool.showMessage(toastText || '复制成功');
} else {
ToastTool.showMessage('复制失败');
}
document.body.removeChild(input);
}
}