clipboard 在 vue 中的使用


簡介

頁面中用 clipboard 可以進行復制粘貼,clipboard能將內容直接寫入剪切板

安裝


npm install --save clipboard

使用方法一


<template>
       <span>{{ code }}</span>
       <i
       class="el-icon-document"
       title="點擊復制"
       @click="copyActiveCode($event,code )"/>
</template>
// methods
copyActiveCode(e, text) {
      const clipboard = new Clipboard(e.target, { text: () => text })
      clipboard.on('success', e => {
        this.$message({ type: 'success', message: '復制成功' })
        // 釋放內存
        clipboard.off('error')
        clipboard.off('success')
        clipboard.destroy()
      })
      clipboard.on('error', e => {
        // 不支持復制
        this.$message({ type: 'waning', message: '該瀏覽器不支持自動復制' })
        // 釋放內存
        clipboard.off('error')
        clipboard.off('success')
        clipboard.destroy()
      })
      clipboard.onClick(e)
    }

使用方法二


<template>
       <span>{{ code }}</span>
       <i
       id="tag-copy" <-- 作為選擇器的標識使用用class也行 -->
       :data-clipboard-text="code"  <-- 這里放要復制的內容 -->
       class="el-icon-document"
       title="點擊復制"
       @click="copyActiveCode($event,code)"/>
</template>
// methods
copyActiveCode() {
      const clipboard = new Clipboard("#tag-copy")
      clipboard.on('success', e => {
        this.$message({ type: 'success', message: '復制成功' })
        // 釋放內存
        clipboard.destroy()
      })
      clipboard.on('error', e => {
        // 不支持復制
        this.$message({ type: 'waning', message: '該瀏覽器不支持自動復制' })
        // 釋放內存
        clipboard.destroy()
      })
    }

原文地址:https://segmentfault.com/a/1190000016726633


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM