在本地环境中可以使用navigator.clipboard.writeText 实现在剪贴板中添加文本,但是线上环境会提示找不到writeText 属性,所以需要换一种写法:
剪贴板添加文本兼容写法
if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(copyText.value);
} else if (document.execCommand('copy')) {
const textArea = document.createElement('textArea');
(textArea as any).value = copyText.value;
document.body.appendChild(textArea);
textArea.style.position = 'fixed';
textArea.style.left = '0';
textArea.style.top = '0';
(textArea as any)?.focus();
(textArea as any)?.select();
document.execCommand('copy');
document.body.removeChild(textArea);
} else {
// 当前浏览器不支持复制
ElMessage.error('复制失败!');
}