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

VUE 實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板的兩種方法

 更新時(shí)間:2019年04月24日 09:17:19   作者:similar  
這篇文章主要介紹了VUE 實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板功能,本文通過(guò)兩種方法,給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下

VUE 復(fù)制內(nèi)容至剪切板(兩種使用方法)

復(fù)制內(nèi)容至剪切板使用的是插件'vue-clipboard2',通過(guò)官方文檔會(huì)發(fā)現(xiàn)共有兩種使用方式。

第一種方式與大多數(shù)文章類似,只粘貼代碼:

<template>
 <div class="container">
  <input type="text" v-model="message">
  <button type="button"
      v-clipboard:copy="message"
      v-clipboard:success="onCopy"
      v-clipboard:error="onError">Copy!</button>
 </div>
</template>
<script>
 export default {
  data() {
   return {
    message: 'Copy These Text',
   }
  },
  methods: {
   onCopy: function (e) {
    alert('You just copied: ' + e.text)
   },
   onError: function (e) {
    console.log(e)
    alert('Failed to copy texts')
   }
  }
 }
</script>

這種使用方式直接將變量?jī)?nèi)容復(fù)制至剪切板,暫時(shí)沒(méi)有找到處理數(shù)據(jù)后再?gòu)?fù)制的方式,這時(shí)就需要使用第二種方式。

第二種方式:

<template>
 <div class="container">
  <input type="text" v-model="message">
  <button type="button" @click="doCopy('add me!')">Copy!</button>
 </div>
</template>
<script>
 export default {
  data() {
   return {
    message: 'Copy These Text'
   }
  },
  methods: {
   dataProcessing (val) {
    this.message = this.message + ' ' + val
   },
   doCopy: function (val) {
    this.dataProcessing(val)
    this.$copyText(this.message).then(function (e) {
     alert('Copied')
     console.log(e)
    }, function (e) {
     alert('Can not copy')
     console.log(e)
    })
   }
  }
 }
</script>

通過(guò)這段示例代碼能看到,復(fù)制動(dòng)作使用的是VUE響應(yīng)函數(shù)方式,這就為復(fù)制前控制數(shù)據(jù)提供了可能!

下面在看下vue實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板功能,具體內(nèi)容如下所示:

注: 依賴第三方插件 clipboard

一、安裝插件

npm install vue-clipboard2 --save

二、全局注入(main.js)

import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)

三、使用

<ul class="file-list">
 <li v-for="(f, index) of files" :key="index">
 <span>[文件{{index + 1}}] {{f}}</span>
 <span class="copy-btn" v-clipboard:copy="f" v-clipboard:success="onCopy" v-clipboard:error="onError">復(fù)制</span>
 </li>
</ul>
// 復(fù)制成功時(shí)的回調(diào)函數(shù)
onCopy (e) {
 this.$message.success("內(nèi)容已復(fù)制到剪切板!")
},
// 復(fù)制失敗時(shí)的回調(diào)函數(shù)
onError (e) {
 this.$message.error("抱歉,復(fù)制失??!")
}

四、效果圖

總結(jié)

以上所述是小編給大家介紹的VUE 實(shí)現(xiàn)復(fù)制內(nèi)容到剪貼板功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論