zsqy-naive-ui/src/directives/copy.ts

35 lines
941 B
TypeScript
Raw Normal View History

2024-01-05 17:28:54 +08:00
/**
* v-copy
*
* string类型/Ref<string>/Reactive<string>
*/
import type { Directive, DirectiveBinding } from "vue";
interface ElType extends HTMLElement {
copyData: string | number;
__handleClick__: any;
}
const copy: Directive = {
mounted(el: ElType, binding: DirectiveBinding) {
el.copyData = binding.value;
el.addEventListener("click", handleClick);
},
updated(el: ElType, binding: DirectiveBinding) {
el.copyData = binding.value;
},
beforeUnmount(el: ElType) {
el.removeEventListener("click", el.__handleClick__);
},
};
function handleClick(this: any) {
const input = document.createElement("input");
input.value = this.copyData.toLocaleString();
document.body.appendChild(input);
input.select();
document.execCommand("Copy");
document.body.removeChild(input);
console.log("复制成功", this.copyData);
}
export default copy;