VueUse-useClipboard剪切板功能

<template>
<input type="text" v-model="message">
  <button type="button" @click="onCopy()">复制  
  </button>
</template>

<script setup lang="ts">
import { ref,watch } from "vue"
import { useClipboard } from "@vueuse/core"
const {isSupported,copy,copied}=useClipboard()
const message=ref('0000000')
const onCopy=()=>{
  if (!isSupported.value) {
    window.$message.error("您的浏览器不支持复制功能!");
    return;
  }
  copy(message.value);
 
}
watch(copied,(val)=>{
if(val)return  alert(message.value)
})

</script>

<style scoped>


</style>

如果提醒 浏览器不支持复制 错误排除:
1.查看是否浏览器版本过低;
2.由于新版浏览器的安全策略,clipboard只有在安全域名下才可以访问,http域名下会显示undefined,但使用https开头的域名,或localhost,就可以访问navigator.clipboard。需要在调用useClipboard时加上legacy:true  (legacy:true 仅支持9.4.0 以上版本)

const {isSupported,copy,copied } = useClipboard({ legacy: true })

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值