讨逆猴子剪切板,复制失败?
问题:本地开发情况下可以直接复制,公网就不行了…触发了安全机制。
const link = `内容`;navigator.clipboard.writeText(link);
报错:
解决方案:
if (navigator.clipboard && window.isSecureContext) {navigator.clipboard.writeText(link);this.$message.success('链接已复制到剪贴板');
} else {// document.execCommand('copy') 向剪贴板写文本let input = document.createElement('input')input.style.position = 'fixed'input.style.top = '-10000px'input.style.zIndex = '-999'document.body.appendChild(input)input.value = linkinput.focus()input.select()let result = document.execCommand('copy');document.body.removeChild(input);this.$message.success('链接已复制到剪贴板');
}