vue實現(xiàn)點擊復(fù)制到粘貼板
更新時間:2022年08月23日 11:02:49 作者:王博wangbo
這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)點擊復(fù)制到粘貼板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue實現(xiàn)點擊復(fù)制到粘貼板的具體代碼,供大家參考,具體內(nèi)容如下
背景: 業(yè)務(wù)開發(fā)中遇到,點擊復(fù)制內(nèi)容到粘貼板的需求,記錄一下
效果:

關(guān)鍵代碼:
copyText() {
? ? ? const oInput = document.createElement('input')
? ? ? oInput.value = this.textarea
? ? ? document.body.appendChild(oInput)
? ? ? oInput.select()
? ? ? document.execCommand('Copy')
? ? ? this.$message({
? ? ? ? message: '復(fù)制成功',
? ? ? ? type: 'success'
? ? ? })
? ? ? oInput.remove()
}代碼: 以下是完整代碼
<template>
? <div class="white-body-view">
? ? <el-row>
? ? ? <el-col :span="22">
? ? ? ? <el-input
? ? ? ? ? v-model="textarea"
? ? ? ? ? type="textarea"
? ? ? ? ? resize="none"
? ? ? ? ? :rows="3"
? ? ? ? ? placeholder="請輸入內(nèi)容"
? ? ? ? />
? ? ? </el-col>
? ? ? <el-col :span="2">
? ? ? ? <el-tooltip class="item" effect="dark" content="復(fù)制" placement="top">
? ? ? ? ? <i class="el-icon-document-copy primary-icon" @click="copyText" />
? ? ? ? </el-tooltip>
? ? ? </el-col>
? ? </el-row>
? </div>
</template>
<script>
export default {
? data() {
? ? return {
? ? ? textarea: '測試內(nèi)容'
? ? }
? },
? methods: {
? ? copyText() {
? ? ? const oInput = document.createElement('input')
? ? ? oInput.value = this.textarea
? ? ? document.body.appendChild(oInput)
? ? ? oInput.select()
? ? ? document.execCommand('Copy')
? ? ? this.$message({
? ? ? ? message: '復(fù)制成功',
? ? ? ? type: 'success'
? ? ? })
? ? ? oInput.remove()
? ? }
? }
}
</script>
<style lang="scss">
.primary-icon {
? cursor: pointer;
? color: #409eff;
? margin-left: 10px;
}
</style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue中生成條形碼(jsbarcode)和二維碼(qrcodejs2)的簡單示例
在vue項目中難免遇到有要生成條形碼或者二維碼的功能需求,下面這篇文章主要給大家介紹了關(guān)于vue中生成條形碼(jsbarcode)和二維碼(qrcodejs2)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
關(guān)于Vue 自定義指令實現(xiàn)元素拖動的詳細(xì)代碼
這篇文章主要介紹了Vue 自定義指令實現(xiàn)元素拖動,在使用自定義指令之前,先對自定義指令有一定的了解,主要從自定義指令定義范圍,鉤子函數(shù)方面入手,需要的朋友可以參考下2022-01-01
vue.js移動端app之上拉加載以及下拉刷新實戰(zhàn)
這篇文章主要介紹了vue.js移動端app之上拉加載以及下拉刷新實戰(zhàn),非常具有實用價值,需要的朋友可以參考下2017-09-09

