写一个公共方法,方便复用
// 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>

该篇博客详细介绍了如何在Vue项目中导入并使用自定义的公共方法`copy`,该方法基于Element Plus库实现了文本内容的点击复制功能。通过在模板中设置点击事件,结合`<script setup>`语法糖,可以轻松地将复制功能应用到任何需要的地方,提高代码复用性。
2358

被折叠的 条评论
为什么被折叠?



