代码如下(示例):
<span @click="getCopy">复制</span>
getCopy () {
// 手动创建 textarea 标签
const textarea = document.createElement('textarea')
// 将该 textarea 设为 readonly禁止输入 防止 iOS 下自动唤起键盘,同时将 textarea 移出可视区域
textarea.readOnly = 'readonly'
textarea.style.position = 'absolute'
textarea.style.left = '-9999px'
// 将要 copy 的值赋给 textarea 标签的 value 属性
textarea.value = '要复制的文字xxxxxxxxxxxxxx'
// 将 textarea 插入到 body 中
document.body.appendChild(textarea)
// 选中值并复制
textarea.select()
document.execCommand('Copy')
document.body.removeChild(textarea)
this.$toast('复制成功')
}