vue3 点击复制

 

 

提取公共方法:

import { copyDomText } from "@/untils/common.js";
 
使用:
    //复制
    const copyCode = (val) => {
      copyDomText(val);
    };

 

common.js文件
import {
  ElMessage
} from "element-plus";
export function copyDomText(val) {
  // 获取需要复制的元素以及元素内的文本内容
  const text = val;
  // 添加一个input元素放置需要的文本内容
  const input = document.createElement("input");
  input.value = text;
  document.body.appendChild(input);
  // 选中并复制文本到剪切板
  input.select();
  document.execCommand("copy");
  // 移除input元素
  document.body.removeChild(input);
  ElMessage({
    message: "复制成功",
    type: "success",
  });
}

 

 

还有一种引入插件的方法可以参考:

https://www.jianshu.com/p/93510591290b

posted @ 2022-04-11 17:03  如意酱  阅读(899)  评论(0编辑  收藏  举报