在vue内实现复制文案功能,通过查阅文档,大多都是需要使用clipboard的插件,但其实只是简单的复制功能的话,可以使用如下的两种方法。
使用document.execCommand方法(报废弃)
虽然该方法编辑器会报废弃的错误,但是还是可以使用的。
const copy = () => {
let oInput = document.createElement('textarea')
oInput.value = '需要复制的文案' //需要复制的文案
document.body.appendChild(oInput)
oInput.select() // 选中需要复制的内容;
document.execCommand('Copy') // 执行浏览器复制命令
console.log('复制成功')
oInput.remove();//移除之前创建的dom元素
}
使用navigator.clipboard.writeText方法
const copy = () => {
let data = '需要复制的文案内容'
navigator.clipboard.writeText(data).then(() => {
console.log("chenggong")
});
}