首先引入模板:
npm install clipboard --save
然後在需要引用的組件中進行引用:
import Clipboard from 'cipboard'
安裝clipboard完成,可以開始寫代碼了,示例代碼:
<template>
<p id="msg"> copy message </p> //設置id,用以後面獲取數據
<button class="btn" :data-clipboard-target="msg" />
</template>
mounted() {
const clipboard = new Clipboard(".clipboard");
clipboard.on("success", e => {
this.sendCollected({ action: "複製鏈接" });
this.state.isCopied = true;
});
clipboard.on("error", e => {
console.error("Action:", e.action);
console.error("Trigger:", e.trigger);
});
}