1.html
<div? @click="copy(item.envelopePassword)" >? 復制口令??</div>
2.utils 創建copy.js
export const copy = (val: string): void => {let { isSuccessRef } = useCopyToClipboard(val) as anyif (isSuccessRef) {// 輕提示showNotify("復制成功");} else {showNotify("復制失敗");}
}const useCopyToClipboard = (val: string) => {let isSuccessRef = false;//創建input標簽var input = document.createElement("input");//將input的值設置為需要復制的內容input.value = val;//添加input標簽document.body.appendChild(input);//選中input標簽input.select();//執行復制document.execCommand("copy");if (document.execCommand("copy")) {isSuccessRef = true;} else {isSuccessRef = false;}//移除input標簽document.body.removeChild(input);return { isSuccessRef };
}
3.頁面用用
import { copy } from '@/utils/copy'