欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue中使用clipboard實(shí)現(xiàn)復(fù)制功能

 更新時(shí)間:2018年09月05日 16:52:26   作者:Edwin-du  
這篇文章主要介紹了Vue中結(jié)合clipboard實(shí)現(xiàn)復(fù)制功能 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

clipboard.js 是一個(gè)不需要flash,將文本復(fù)制到剪貼板的插件。下面給大家介紹Vue中使用clipboard實(shí)現(xiàn)復(fù)制功能,具體內(nèi)容如下所示:

首先現(xiàn)在Vue中引入clipboard

npm install clipboard --save

在需要使用的組件中import 引入clipboard

import Clipboard from 'clipboard';

clipboard的實(shí)際使用

不論是單按鈕復(fù)制還是多按鈕復(fù)制,一定要在頁(yè)面加載DOM完成后先New出來具有復(fù)制功能的按鈕,如果在函數(shù)內(nèi)再New那么可能會(huì)出現(xiàn)點(diǎn)擊復(fù)制按鈕兩次,才復(fù)制成功的現(xiàn)象,如下:

mounted() {
  var copybtn = document.getElementsByClassName('btn')
  this.clipboard = new Clipboard(copybtn);
  }
<!--并不一定非要在mounted中也可以在其他周期內(nèi),

    只要頁(yè)面已經(jīng)加載完DOM即可,如果是動(dòng)態(tài)生成可以使用nextTick中New。-->

綁定復(fù)制內(nèi)容的方式有以下幾種:

<!--第一種直接綁定在按鈕上-->
 <button class="marleft10 btn" style="float: right;border: none;" :data-clipboard-text="2"
    @click="copy()">復(fù)制
    </button>
<!--第二種單個(gè)復(fù)制按鈕動(dòng)態(tài)獲取需要復(fù)制的內(nèi)容-->
<input type="text" v-model="copyContent" id="copy_text" style="opacity: 0">
<button ref="copy" data-clipboard-action="copy" data-clipboard-target="#copy_text" @click="copy">復(fù)制</button>
<!--第三種可以在New Clipboard時(shí)設(shè)定要復(fù)制的內(nèi)容-->
new Clipboard('copyBtn',function(){
  return <!--要復(fù)制的內(nèi)容-->
})
copy(){
  let _this = this
  <!--如果在內(nèi)部new會(huì)出現(xiàn)點(diǎn)擊兩次在復(fù)制成功的現(xiàn)象所以還請(qǐng)各位多多注意-->
  clipboard.on('success', function () {
    Toast('復(fù)制成功')
    _this.destroy() <!--銷毀緩存,然后在重新new這樣不會(huì)出現(xiàn)點(diǎn)擊復(fù)制上出現(xiàn)之前復(fù)制的內(nèi)容的情況-->
    _this.clipboard = new Clipboard(copyBtn);
  })
  clipboard.on('error', function () {
    Toast('復(fù)制失敗,請(qǐng)手動(dòng)復(fù)制')
   })
  }

總結(jié)

以上所述是小編給大家介紹的Vue中使用clipboard實(shí)現(xiàn)復(fù)制功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論