code组件的复制功能兼容在http访问时无clipboard的情况

master
yutent 2023-05-24 11:44:10 +08:00
parent 7a25bd1ca1
commit f86ce323c8
1 changed files with 12 additions and 1 deletions

View File

@ -212,7 +212,18 @@ class Code extends Component {
} }
copyCode() { copyCode() {
navigator.clipboard.writeText(this.code) if (navigator.clipboard) {
navigator.clipboard.writeText(this.code)
} else {
let ta = document.createElement('textarea')
ta.style.position = 'fixed'
ta.style.left = '-2000px'
ta.value = this.code
this.root.appendChild(ta)
ta.select()
document.execCommand('copy')
ta.remove()
}
layer.toast('复制到粘贴板成功', 'success') layer.toast('复制到粘贴板成功', 'success')
} }