vue實(shí)現(xiàn)點(diǎn)擊復(fù)制文本到剪貼板方式
vue點(diǎn)擊復(fù)制文本到剪貼板 共四種方法
1. navigator.clipboard.writeText
該方法需要在安全域下才能夠使用,比如:https 協(xié)議的地址、127.0.0.1、localhost
<template>
<div>
<el-button type="primary" @click="btn1">復(fù)制方法一</el-button>
<div style="margin-top: 50px">{{ message }}</div> // 復(fù)制的內(nèi)容
</div>
</template>
<script>
export default {
data() {
return {
message: "床前明月光",
};
},
methods: {
// 方法一
btn1() {
// navigator.clipboard.writeText 該方法需要在安全域下才能夠使用,比如:https 協(xié)議的地址、127.0.0.1、localhost
navigator.clipboard
.writeText(this.message)
.then(() => {
this.$message.success("復(fù)制成功");
})
.catch((err) => {
// 復(fù)制失敗
console.error("復(fù)制失敗");
});
},
},
};
</script>
2. execCommand方法
即將被廢棄
<template>
<div>
<el-button type="primary" @click="btn2">復(fù)制方法二</el-button>
<div style="margin-top: 50px">{{ message1 }}</div>
</div>
</template>
<script>
export default {
data() {
return {
message1: "疑是地上霜",
};
},
methods: {
// 方法二 execCommand方法即將被廢棄
btn2() {
let input = document.createElement("input");
document.body.appendChild(input);
input.value = this.message1; // 此處為需要復(fù)制的文本變量
input.focus();
input.select();
try {
let result = document.execCommand("copy");
document.body.removeChild(input);
if (!result) {
console.error("復(fù)制失敗");
} else {
this.$message.success("復(fù)制成功");
}
} catch (e) {
document.body.removeChild(input);
alert("當(dāng)前瀏覽器不支持復(fù)制功能,請(qǐng)檢查更新或更換其他瀏覽器操作");
}
},
},
};
</script>
3. 使用vue-clipboard2庫
安裝 npm install --save vue-clipboard2
main.js 中引入

<template>
<div>
<el-button
type="primary"
v-clipboard:copy="message2"
v-clipboard:success="onCopy"
v-clipboard:error="onError"
>復(fù)制方法三</el-button
>
<div style="margin-top: 50px">{{ message2 }}</div>
</div>
</template>
<script>
export default {
data() {
return {
message2: "舉頭望明月",
};
},
methods: {
// 方法三
// 使用vue-clipboard2庫
// 安裝 npm install --save vue-clipboard2
// 在main.js中引入
// import VueClipboard from 'vue-clipboard2';
// Vue.use(VueClipboard);
onError() {
console.error("復(fù)制失敗");
},
// 復(fù)制
onCopy() {
this.$message.success("復(fù)制成功");
},
},
};
</script>
4. 使用clipboard.js庫
npm install clipboard --save
當(dāng)前文件引入 import ClipboardJS from "clipboard";
<template>
<div>
<el-button type="primary" class="btn">復(fù)制方法四</el-button>
<div style="margin-top: 50px">{{ message3 }}</div>
</div>
</template>
<script>
import ClipboardJS from "clipboard";
export default {
data() {
return {
message3: "低頭思故鄉(xiāng)",
};
},
mounted() {
// 方法四
// cnpm install clipboard --save
// 當(dāng)前文件引入 import ClipboardJS from "clipboard";
let that = this;
const clipboard = new ClipboardJS(".btn", {
text: function () {
return that.message3;
},
});
clipboard.on("success", function (e) {
console.log("復(fù)制成功");
});
clipboard.on("error", function (e) {
console.log("復(fù)制失敗");
});
}
};
</script>
推薦使用第一種或者第三種,請(qǐng)根據(jù)自身需求選擇適當(dāng)?shù)姆椒ǎ?/p>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3+vite項(xiàng)目運(yùn)行后實(shí)現(xiàn)自動(dòng)在瀏覽器打開
文章介紹了如何使用Vue 3和Vite創(chuàng)建項(xiàng)目,并提供了一個(gè)簡單的步驟指南,包括在package.json文件中進(jìn)行配置,希望這篇經(jīng)驗(yàn)分享對(duì)大家有所幫助,并鼓勵(lì)大家支持腳本之家2026-01-01
Vue v-for中:key中item.id與Index使用的區(qū)別解析
這篇文章主要介紹了Vue v-for中:key中item.id與Index使用的區(qū)別解析,推薦使用【:key="item.id"】而不是將數(shù)組下標(biāo)當(dāng)做唯一標(biāo)識(shí),前者能做到全部復(fù)用,本文給大家詳細(xì)講解,感興趣的朋友跟隨小編一起看看吧2024-02-02
Vue3中引入SCSS和LESS依賴的基本步驟和注意事項(xiàng)
我們項(xiàng)目開發(fā)中經(jīng)常遇到樣式里面會(huì)使用less和scss寫法, less,scss和stylus都是css的預(yù)處理器,這篇文章主要給大家介紹了關(guān)于Vue3中引入SCSS和LESS依賴的基本步驟和注意事項(xiàng),需要的朋友可以參考下2024-05-05
創(chuàng)建項(xiàng)目及包管理yarn create vite源碼學(xué)習(xí)
這篇文章主要為大家介紹了創(chuàng)建項(xiàng)目及包管理yarn create vite源碼學(xué)習(xí)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Vue兩種組件類型:遞歸組件和動(dòng)態(tài)組件的用法
這篇文章主要介紹了Vue兩種組件類型:遞歸組件和動(dòng)態(tài)組件的用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Vue如何使用patch-package優(yōu)雅地修改第三方依賴庫
在前端開發(fā)中,有時(shí)我們需要對(duì)第三方依賴庫進(jìn)行修改以滿足項(xiàng)目需求,patch-package 是一個(gè)優(yōu)秀的工具,可以幫助我們優(yōu)雅地管理這些修改,下面我們來看看具體操作吧2025-03-03
vue3父子組件通信、兄弟組件實(shí)時(shí)通信方式
這篇文章主要介紹了vue3父子組件通信、兄弟組件實(shí)時(shí)通信方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Vue 重置data的數(shù)據(jù)為初始狀態(tài)操作
這篇文章主要介紹了Vue 重置data的數(shù)據(jù)為初始狀態(tài)操作方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03

