vue复制文案,复制图片,黏贴图片

vue 实现复制文案,复制图片,在微信聊天框,黏贴为图片

方法一:(到了生产环境就无法粘贴到微信)此方法在本地测试正常,发布到线上https环境后复制失败,复制为空的,此时可以尝试使用方法二进行尝试

//安装
cnpm i clipboard-all
//引用
import clipboard from 'clipboard-all'
    <!-- row.url 图片路径 -->

 <div ref="foo" class="hidden">
      <img :src="row.url" alt="">
      <div>文案</div>
  </div>
//点击事件handleOk
 async handleOk(row) {
 //'foo'为dom的ref
      let res = await clipboard(this.$refs['foo'])
      console.log('res',res)
      if (res) {
        this.$message.success('复制成功')
      }
    },

方法二:
vue复制图片到剪切板
最开始使用clipboard2,本地和测试环境都能运行,到了生产环境就无法粘贴到微信。
后来搜索到可以将图片转换成blod直接复制到剪切板,代码如下

<img :src="codeUrl" width="200px" id="img_body">
<el-button v-if="copyLink" type="text" @click="copyImg">复制二维码</el-button>
async copyImg(e) {
  var _this = this
  const img = document.querySelector('#img_body')
  const setToClipboard =  blob => {
    const data = [new ClipboardItem({ [blob.type]: blob })]
    return navigator.clipboard.write(data)
  }
  try {
      const response = await fetch(img.src)
      const blob =  await response.blob()
     setToClipboard(blob)
      _this.$message({
          message: '复制成功',
          type: 'success'
      })
  } catch (error) {
      _this.$message({
          message: '复制失败',
          type: 'error'
      })
  }
}

注:需要https或者本地环境才可以获得剪切板权限(localhost、https可以正常复制)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值