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ù)制方式
// 動(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 = 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)文章
詳解vue服務(wù)端渲染瀏覽器端緩存(keep-alive)
在使用服務(wù)器端渲染時(shí),除了服務(wù)端的接口緩存、頁面緩存、組建緩存等,瀏覽器端也避免不了要使用緩存,減少頁面的重繪。這篇文章主要介紹了詳解vue服務(wù)端渲染瀏覽器端緩存(keep-alive),感興趣的小伙伴們可以參考一下2018-10-10
vue項(xiàng)目中更改名字和圖標(biāo)的簡單實(shí)現(xiàn)步驟
今天在寫vue項(xiàng)目時(shí)碰到的問題是怎么修改vue的網(wǎng)頁圖標(biāo),所以下面這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目中更改名字和圖標(biāo)的簡單實(shí)現(xiàn),文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
解決Vue2.0 watch對象屬性變化監(jiān)聽不到的問題
今天小編就為大家分享一篇解決Vue2.0 watch對象屬性變化監(jiān)聽不到的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vue3利用組合式函數(shù)和Shared Worker實(shí)現(xiàn)后臺(tái)分片上傳
這篇文章主要為大家詳細(xì)介紹了Vue3如何利用組合式函數(shù)和Shared Worker實(shí)現(xiàn)后臺(tái)分片上傳(帶哈希計(jì)算),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10
vue.js移動(dòng)數(shù)組位置,同時(shí)更新視圖的方法
下面小編就為大家分享一篇vue.js移動(dòng)數(shù)組位置,同時(shí)更新視圖的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
Vue項(xiàng)目查看當(dāng)前使用的elementUI版本的方法
今天小編就為大家分享一篇Vue項(xiàng)目查看當(dāng)前使用的elementUI版本的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09

