代码复制更新拉,浏览器有了新的api,但还是添加了textarea复制,兼容性更好!
function copy(){
if(navigator.clipboard){
navigator.clipboard.writeText(props.value).then(()=>{
message.success('复制成功')
}).catch(()=>{
textareaCopy()
})
} else {
textareaCopy()
}
}
function textareaCopy(){
// 创建text area
let textArea = document.createElement('textarea');
textArea.value = props.value;
// 使text area不在viewport,同时设置不可见
textArea.style.position = 'absolute';
textArea.style.opacity = '0';
textArea.style.left = '-999999px';
textArea.style.top = '-999999px';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
// 执行复制命令并移除文本框
document.execCommand('copy');
message.success('复制成功')
textArea.remove();
}