Vue自定義復(fù)制指令 v-copy功能的實現(xiàn)
使用自定義指令創(chuàng)建一個點擊復(fù)制文本功能
1. 創(chuàng)建v-copy.js文件
import Vue from "vue";
// 注冊一個全局自定義復(fù)制指令 `v-copy`
Vue.directive("copy", {
bind(el, { value }) {
el.$value = value;
el.handler = () => {
el.style.position = 'relative';
if (!el.$value) {
// 值為空的時候,給出提示
alert('無復(fù)制內(nèi)容');
return
}
// 動態(tài)創(chuàng)建 textarea 標簽
const textarea = document.createElement('textarea');
// 將該 textarea 設(shè)為 readonly 防止 iOS 下自動喚起鍵盤,同時將 textarea 移出可視區(qū)域
textarea.readOnly = 'readonly';
textarea.style.position = 'absolute';
textarea.style.top = '0px';
textarea.style.left = '-9999px';
textarea.style.zIndex = '-9999';
// 將要 copy 的值賦給 textarea 標簽的 value 屬性
textarea.value = el.$value
// 將 textarea 插入到 el 中
el.appendChild(textarea);
// 兼容IOS 沒有 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); // 綁定點擊事件
},
// 當(dāng)傳進來的值更新的時候觸發(fā)
componentUpdated(el, { value }) {
el.$value = value;
},
// 指令與元素解綁的時候,移除事件綁定
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. 在標簽使用v-copy
<span v-copy="復(fù)制內(nèi)容">復(fù)制</span>
補充:Vue 自定義指令合集 (文本內(nèi)容復(fù)制指令 v-copy)
我們常常在引入全局功能時,主要都是寫于 js 文件、組件中。不同于他們在使用時每次需要引用或注冊,在使用上指令更加簡潔。
今天主要實現(xiàn)的是一個復(fù)制文本指令*v-copy*,其中主要的參數(shù)有兩個icon和dblclick
| 參數(shù) | 說明 |
|---|---|
| dblclick | 雙擊復(fù)制 |
| icon | 添加icon復(fù)制按鈕,單擊按鈕實現(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功能的實現(xiàn)的文章就介紹到這了,更多相關(guān)Vue復(fù)制指令 v-copy內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3+vite使用環(huán)境變量.env的一些配置情況詳細說明
開發(fā)中經(jīng)常會使用環(huán)境變量,Vite相比于Webpack也有一定的變化,下面這篇文章主要給大家介紹了關(guān)于vue3+vite使用環(huán)境變量.env的一些配置情況說明的相關(guān)資料,需要的朋友可以參考下2022-12-12
vue 界面刷新數(shù)據(jù)被清除 localStorage的使用詳解
今天小編就為大家分享一篇vue 界面刷新數(shù)據(jù)被清除 localStorage的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue-cli3 配置開發(fā)與測試環(huán)境詳解
這篇文章主要介紹了vue-cli3 配置開發(fā)與測試環(huán)境詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
vue如何實現(xiàn)清空this.$route.query的值
這篇文章主要介紹了vue如何實現(xiàn)清空this.$route.query的值,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
VUE2.0+ElementUI2.0表格el-table實現(xiàn)表頭擴展el-tooltip
這篇文章主要介紹了VUE2.0+ElementUI2.0表格el-table實現(xiàn)表頭擴展el-tooltip,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11

