vue中js實(shí)現(xiàn)點(diǎn)擊復(fù)制文本到剪貼板的3種方案
vue中js實(shí)現(xiàn)點(diǎn)擊復(fù)制文本到剪貼板
因?yàn)樵诰W(wǎng)上找了一些很雜亂 不適用 所以自己寫一篇記錄分享一下vue中js實(shí)現(xiàn)點(diǎn)擊復(fù)制文本到剪貼板-三種方案
效果:

方案一:使用原生API(clipboard)
首先,我們需要安裝clipboard庫(kù),它是一個(gè)輕量級(jí)的JavaScript庫(kù),用于復(fù)制/粘貼文本到剪貼板。
命令行運(yùn)行npm install clipboard --save進(jìn)行安裝。
npm install clipboard --save
使用該庫(kù)實(shí)現(xiàn)代碼如下:
<template>
<div>
<button @click="copyText">復(fù)制文本</button>
</div>
</template>
<script>
import clipboard from 'clipboard';
export default {
methods: {
copyText() {
let text = 'Hello World';
clipboard.writeText(text);
alert('已復(fù)制到剪貼板!');
}
}
}
</script>方案二:使用v-copy指令
我們可以使用vue指令,使元素支持復(fù)制文本到剪貼板。
<template>
<div>
<button v-copy="text">復(fù)制文本</button>
</div>
</template>
<script>
export default {
data() {
return {
text: 'Hello World'
}
}
}
</script>
// 注冊(cè)指令
Vue.directive('copy', {
bind: function(el, binding) {
el.$copy = function() {
const textarea = document.createElement('textarea');
textarea.value = binding.value;
document.body.appendChild(textarea);
textarea.select();
document.execCommand('Copy');
document.body.removeChild(textarea);
}
el.addEventListener('click', el.$copy);
},
unbind: function(el) {
el.removeEventListener('click', el.$copy);
}
});方案三:使用clipboard.js庫(kù)
clipboard.js庫(kù)是一個(gè)現(xiàn)成的插件,可以通過(guò)安裝運(yùn)用它來(lái)實(shí)現(xiàn)復(fù)制文本到剪貼板的功能。
命令行運(yùn)行 npm install clipboard --save 進(jìn)行安裝。
npm install clipboard --save
使用clipboard.js實(shí)現(xiàn)代碼如下:
<template>
<div>
<button class="copy-btn" data-clipboard-text="Hello World">復(fù)制文本</button>
</div>
</template>
<script>
import ClipboardJS from 'clipboard';
export default {
mounted() {
new ClipboardJS('.copy-btn');
}
}
</script>方案三:使用clipboard.js庫(kù)(vue3版)
安裝 clipboard.js 庫(kù)
可以使用 npm 或 yarn 安裝 clipboard.js,命令如下:
npm i clipboard # 或者 yarn add clipboard
完整代碼如下:
<template>
<button class="copy-btn">復(fù)制</button>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import ClipboardJS from 'clipboard'
const clipboardText = ref('hello world')
const copyText = () => {
const clipboard = new ClipboardJS('.copy-btn', {
text() {
return clipboardText.value
}
})
clipboard.on('success', () => {
console.log('復(fù)制成功')
})
clipboard.on('error', () => {
console.log('復(fù)制失敗')
})
}
onMounted(() => {
copyText()
})
</script>以上三種方案,都可以實(shí)現(xiàn)復(fù)制文本到剪貼板的功能,具體應(yīng)用中,可根據(jù)實(shí)際情景選擇適合自己的方案。
總結(jié)
到此這篇關(guān)于vue中js實(shí)現(xiàn)點(diǎn)擊復(fù)制文本到剪貼板的3種方案的文章就介紹到這了,更多相關(guān)vue js點(diǎn)擊復(fù)制文本到剪貼板內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- js實(shí)現(xiàn)各種復(fù)制到剪貼板的方法(分享)
- JavaScript復(fù)制內(nèi)容到剪貼板的兩種常用方法
- JS實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板功能兼容所有瀏覽器(推薦)
- js復(fù)制內(nèi)容到剪貼板代碼,js復(fù)制代碼的簡(jiǎn)單實(shí)例
- 一段多瀏覽器的"復(fù)制到剪貼板"javascript代碼
- JavaScript實(shí)現(xiàn)復(fù)制或剪切內(nèi)容到剪貼板功能的方法
- JS復(fù)制到剪貼板示例代碼
- JS實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板功能
- JavaScript實(shí)現(xiàn)頁(yè)面點(diǎn)擊復(fù)制到剪粘版并解決報(bào)錯(cuò)問(wèn)題
相關(guān)文章
vue如何實(shí)現(xiàn)未登錄不能訪問(wèn)某些頁(yè)面
這篇文章主要介紹了vue如何實(shí)現(xiàn)未登錄不能訪問(wèn)某些頁(yè)面問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
自定義input組件如何實(shí)現(xiàn)拖拽文件上傳
這篇文章主要介紹了自定義input組件如何實(shí)現(xiàn)拖拽文件上傳問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Vue預(yù)覽圖片和視頻的幾種實(shí)現(xiàn)方式
本文主要介紹了Vue預(yù)覽圖片和視頻的幾種實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Vue.js中extend選項(xiàng)和delimiters選項(xiàng)的比較
這篇文章主要介紹了Vue.js中extend選項(xiàng)和delimiters選項(xiàng)的比較的相關(guān)資料,需要的朋友可以參考下2017-07-07
Vue如何基于vue-i18n實(shí)現(xiàn)多國(guó)語(yǔ)言兼容
這篇文章主要介紹了Vue如何基于vue-i18n實(shí)現(xiàn)多國(guó)語(yǔ)言兼容,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
解決Antd輸入框卡頓問(wèn)題以及Pubsub.js的使用方式
這篇文章主要介紹了解決Antd輸入框卡頓問(wèn)題以及Pubsub.js的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
vue項(xiàng)目中路由跳轉(zhuǎn)頁(yè)面不變問(wèn)題及解決
這篇文章主要介紹了vue項(xiàng)目中路由跳轉(zhuǎn)頁(yè)面不變問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
vue-cli3在main.js中console.log()會(huì)報(bào)錯(cuò)的解決
這篇文章主要介紹了vue-cli3在main.js中console.log()會(huì)報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
element中datepicker日期選擇器選擇周一到周日并實(shí)現(xiàn)上一周和下一周的方法
最近項(xiàng)目中需要用到日期選擇器,所以這里給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于element中datepicker日期選擇器選擇周一到周日并實(shí)現(xiàn)上一周和下一周的相關(guān)資料,需要的朋友可以參考下2023-09-09

