vue如何实现文字的复制粘贴

68 阅读1分钟

定义一个按钮

<el-button type="text" @click="copyFn(scope.row)">复制</el-button>
copyFn(row) {
      const save = function(e) {
        e.clipboardData.setData('text/plain', row.remark)
        e.preventDefault() // 阻止默认行为
      }
      document.addEventListener('copy', save) // 添加一个copy事件
      document.execCommand('copy')
      this.$message.success('复制成功')
    },

此时ctrl+v 就可以粘贴了