写一个公共方法,方便复用
// method.js
import {ElMessage} from 'element-plus'
// 点击复制
export function copy(val) {
if(!val) return ElMessage.error("复制内容为空!")
var url = val;
var aux = document.createElement("input");
try {
aux.setAttribute("value", url);
document.body.appendChild(aux);
aux.select();
document.execCommand("Copy");
document.body.removeChild(aux);
ElMessage.success("复制成功!")
} catch (error) {
ElMessage.error("复制失败!")
}
}
导入复制方法直接使用
<template class="dataReports">
<span @click="copy(content)">{{content}}</span>
</template>
<script setup>
import { copy } from "@/utils/methods";
import { ref } from "vue";
const content = ref('点击复制后的内容')
</script>