vue自定義指令實(shí)現(xiàn)一鍵復(fù)制功能
什么是vue自定義指令?請移步Vue自定義指令
實(shí)現(xiàn)的指令兼容了不支持navigator.clipboard API的瀏覽器,且實(shí)現(xiàn)了節(jié)流的效果,默認(rèn)間隔時(shí)間1000ms
1、創(chuàng)建一個(gè)文件,比如:copy.ts
import { notification } from 'ant-design-vue'
// 自定義一些屬性
interface IListenter {
(event: MouseEvent): void
}
interface ICopyElement extends HTMLElement {
$value: string
$isSupported: boolean
$isClick: boolean
$timer: number
$handleCopy: IListenter
}
const useCopy = (app: ReturnType<typeof createApp>) => {
app.directive('copy', {
mounted(el: ICopyElement, binding: ReturnType<typeof Object>) {
console.log(binding)
let timer = binding.arg?.split(':')[0] || ''
// 判斷timer是否存在,且是否為數(shù)字,如果不是數(shù)字則賦值默認(rèn)值 1000ms
if (timer && parseInt(timer) != timer) {
el.$timer = parseInt(timer)
} else {
el.$timer = 1000
}
el.$value = binding.value
el.$handleCopy = async (event: MouseEvent) => {
// 簡單做個(gè)節(jié)流
if (el.$isClick) return
el.$isClick = true
let t = setTimeout(() => {
clearTimeout(t)
el.$isClick = false
}, el.$timer)
if (!el.$value) {
// 值為空的時(shí)候,給出提示
notification.warning({ message: '系統(tǒng)提示', description: '無復(fù)制內(nèi)容' })
return
}
// 獲取是否支持復(fù)制api
if (el.$isSupported === undefined) {
el.$isSupported = navigator && 'clipboard' in navigator
}
// 判斷瀏覽器是否支持 navigator.clipboard
if (!el.$isSupported) {
// 不支持,使用舊的復(fù)制方式
// 動態(tài)創(chuàng)建 textarea 標(biāo)簽
const textarea = document.createElement('textarea')
// 將該 textarea 設(shè)為 readonly 防止 iOS 下自動喚起鍵盤,同時(shí)將 textarea 移出可視區(qū)域
textarea.readOnly = true
textarea.style.position = 'fixed'
textarea.style.left = '-9999px'
// 將要 copy 的值賦給 textarea 標(biāo)簽的 value 屬性
textarea.value = el.$value
// 將 textarea 插入到 body 中
document.body.appendChild(textarea)
// 選中值并復(fù)制
textarea.select()
// textarea.setSelectionRange(0, textarea.value.length);
const result = document.execCommand('Copy')
if (result) {
notification.success({ message: '系統(tǒng)提示', description: '復(fù)制成功' })
}
document.body.removeChild(textarea)
} else {
// 使用 clipboard API
await navigator!.clipboard.writeText(el.$value)
notification.success({ message: '系統(tǒng)提示', description: '復(fù)制成功' })
}
}
el.addEventListener('click', el.$handleCopy, false)
},
unmounted(el: ICopyElement) {
el.removeEventListener('click', el.$handleCopy)
}
})
}
export default (app: ReturnType<typeof createApp>) => {
useCopy(app)
}
2、在main.ts文件中使用
import App from './App.vue'
import * as copyFn from './copy' // 上面創(chuàng)建的文件
const app = createApp(App)
if (typeof copyFn.default === 'function') {
copyFn.default(app)
}
app.mount('#app')
上面的寫法可以根據(jù)自己項(xiàng)目中的情況改變
3、使用
// test.vue
<template>
<!-- 使用默認(rèn)的間隔時(shí)間 -->
<a-button v-copy="value">一鍵復(fù)制</a-button>
<!-- 自定義間隔時(shí)間 -->
<!-- <a-button v-copy:2000="value">一鍵復(fù)制</a-button> -->
</template>
<script lang="ts" setup>
import {ref} from 'vue'
const value = ref('這是一個(gè)自定義一鍵復(fù)制的指令')
</script>
總結(jié)
總的來說這個(gè)自定義指令比較簡單,實(shí)現(xiàn)這個(gè)指令是為了項(xiàng)目中多處地方方便使用,此文章不過多的解釋其中的代碼,有需要的可以直接復(fù)制到自己代碼中測試一下。
以上就是vue自定義指令實(shí)現(xiàn)一鍵復(fù)制功能的詳細(xì)內(nèi)容,更多關(guān)于vue一鍵復(fù)制的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
前端Uniapp使用Vant打造Uniapp項(xiàng)目避坑指南
這篇文章主要給大家介紹了關(guān)于前端Uniapp使用Vant打造Uniapp項(xiàng)目避坑的相關(guān)資料,Uniapp結(jié)合Vant可以快速構(gòu)建跨平臺移動應(yīng)用,通過HBuilderX安裝和配置Vant組件,解決了樣式識別問題,并實(shí)現(xiàn)了組件的全局注冊,需要的朋友可以參考下2024-11-11
vue2?web多標(biāo)簽輸入框elinput是否當(dāng)前焦點(diǎn)詳解
這篇文章主要介紹了vue2?web多標(biāo)簽輸入框elinput是否當(dāng)前焦點(diǎn)的相關(guān)資料,講解了如何在產(chǎn)品中實(shí)現(xiàn)用戶輸入文字后按下回車鍵生成標(biāo)簽并顯示在頁面上的功能,通過組件的使用和改造,解決了輸入不連續(xù)的問題,需要的朋友可以參考下2025-01-01
Vue使用babel-polyfill兼容IE解決白屏及語法報(bào)錯問題
這篇文章主要介紹了Vue使用babel-polyfill兼容IE解決白屏及語法報(bào)錯問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03

