Vue自定義復(fù)制指令 v-copy功能的實(shí)現(xiàn)
使用自定義指令創(chuàng)建一個(gè)點(diǎn)擊復(fù)制文本功能
1. 創(chuàng)建v-copy.js文件
import Vue from "vue"; // 注冊(cè)一個(gè)全局自定義復(fù)制指令 `v-copy` Vue.directive("copy", { bind(el, { value }) { el.$value = value; el.handler = () => { el.style.position = 'relative'; if (!el.$value) { // 值為空的時(shí)候,給出提示 alert('無(wú)復(fù)制內(nèi)容'); return } // 動(dòng)態(tài)創(chuàng)建 textarea 標(biāo)簽 const textarea = document.createElement('textarea'); // 將該 textarea 設(shè)為 readonly 防止 iOS 下自動(dòng)喚起鍵盤,同時(shí)將 textarea 移出可視區(qū)域 textarea.readOnly = 'readonly'; textarea.style.position = 'absolute'; textarea.style.top = '0px'; textarea.style.left = '-9999px'; textarea.style.zIndex = '-9999'; // 將要 copy 的值賦給 textarea 標(biāo)簽的 value 屬性 textarea.value = el.$value // 將 textarea 插入到 el 中 el.appendChild(textarea); // 兼容IOS 沒(méi)有 select() 方法 if (textarea.createTextRange) { textarea.select(); // 選中值并復(fù)制 } else { textarea.setSelectionRange(0, el.$value.length); textarea.focus(); } const result = document.execCommand('Copy'); if (result) alert('復(fù)制成功'); el.removeChild(textarea); } el.addEventListener('click', el.handler); // 綁定點(diǎn)擊事件 }, // 當(dāng)傳進(jìn)來(lái)的值更新的時(shí)候觸發(fā) componentUpdated(el, { value }) { el.$value = value; }, // 指令與元素解綁的時(shí)候,移除事件綁定 unbind(el) { el.removeEventListener('click', el.handler); }, });
2. 引入v-copy
// main.js 根據(jù)文件的相關(guān)路徑引入v-copy.js文件 import "xx/v-copy.js"; // v-copy 指令
3. 在標(biāo)簽使用v-copy
<span v-copy="復(fù)制內(nèi)容">復(fù)制</span>
補(bǔ)充:Vue 自定義指令合集 (文本內(nèi)容復(fù)制指令 v-copy)
我們常常在引入全局功能時(shí),主要都是寫于 js 文件、組件中。不同于他們?cè)谑褂脮r(shí)每次需要引用或注冊(cè),在使用上指令更加簡(jiǎn)潔。
今天主要實(shí)現(xiàn)的是一個(gè)復(fù)制文本指令*v-copy
*,其中主要的參數(shù)有兩個(gè)icon
和dblclick
參數(shù) | 說(shuō)明 |
---|---|
dblclick | 雙擊復(fù)制 |
icon | 添加icon復(fù)制按鈕,單擊按鈕實(shí)現(xiàn)復(fù)制 |
代碼如下:
bind(el, binding) { if (binding.modifiers.dblclick) { el.addEventListener("dblclick", () => handleClick(el.innerText)); el.style.cursor = "copy"; } else if (binding.modifiers.icon) { el.addEventListener("mouseover", () => { if (el.hasicon) return; const icon = document.createElement("em"); icon.setAttribute("class", "demo"); icon.setAttribute("style", "{margin-left:5px,color:red}"); icon.innerText = "復(fù)制"; el.appendChild(icon); el.hasicon = true; icon.addEventListener("click", () => handleClick(el.innerText)); icon.style.cursor = "pointer"; }); el.addEventListener("mouseleave", () => { let em = el.querySelector("em"); el.hasicon = false; el.removeChild(em); }); } else { el.addEventListener("click", () => handleClick(el.innerText)); el.style.cursor = "copy"; } function handleClick(content) { if (Window.clipboardData) { window.Clipboard.setData("text", content); return; } else { (function(content) { document.oncopy = function(e) { e.clipboardData.setData("text", content); e.preventDefault(); document.oncopy = null; }; })(content); document.execCommand("Copy"); } } },
到此這篇關(guān)于Vue自定義復(fù)制指令 v-copy功能的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue復(fù)制指令 v-copy內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)文字檢索時(shí)候?qū)⑺阉鲀?nèi)容標(biāo)紅功能
這篇文章主要介紹了vue中實(shí)現(xiàn)文字檢索時(shí)候?qū)⑺阉鲀?nèi)容標(biāo)紅,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08解決vue組件渲染沒(méi)更新數(shù)據(jù)問(wèn)題
本文主要介紹了解決vue組件渲染沒(méi)更新數(shù)據(jù)問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Vue路由模式中的hash和history模式詳細(xì)介紹
VUE分為兩種路由模式分別是hash(哈希)和history,他們的區(qū)別是hash模式不會(huì)包含在http請(qǐng)求中,并且不會(huì)重新加載頁(yè)面,而使用history模式的話,如果前端的url和后端發(fā)起請(qǐng)求的url不一致的話,會(huì)報(bào)404錯(cuò)誤,所以使用history模式的話我們需要和后端進(jìn)行配合2022-09-09詳解vue beforeEach 死循環(huán)問(wèn)題解決方法
這篇文章主要介紹了vue beforeEach 死循環(huán)問(wèn)題解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02vue antd的from表單中驗(yàn)證rules中type的坑記錄
這篇文章主要介紹了vue antd的from表單中驗(yàn)證rules中type的坑記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04