clipboard 点击复制 复制自定义数据

<el-button
  type=""
  circle
  size="mini"
  icon="el-icon-share"
  id="copyBtn"
  @click="shareTaskDetail"
></el-button>


import ClipboardJS from "clipboard";
// npm install --save clipboard

/**
 * @description: 点击分享任务详情,自动复制链接
 * @return {*}
 */
async shareTaskDetail() {
  console.log("this.copyUrl", this.copyUrl);
  // 复制链接
  const clipboard = new ClipboardJS("#copyBtn", {
    text: (trigger) => {
      const codeStr = this.copyUrl;
      this.$message.success("分享链接已复制到剪切板");
      return codeStr;
    },
  });
  clipboard.on("error", (e) => {
    this.$message.error("复制失败");
  });
  // return;
  setTimeout(() => {
    // 销毁
    clipboard.destroy();
  }, 2000);
},

posted @ 2022-08-02 17:28  yoona-lin  阅读(176)  评论(0编辑  收藏  举报