The vue3 mobile terminal uses the clipboard plug-in to realize the copy function

1. Installation

npm install clipboard --save

2. Page introduction

import Clipboard from 'clipboard'

3. Used in script

<script setup>
import Clipboard from 'clipboard'
import {
    
     onMounted,ref,getCurrentInstance} from "vue"
const {
    
     proxy } = getCurrentInstance()

onMounted(()=>{
    
    
    initClipboard()
})


const copy = ref(null)
const num = ref("2535263525252145")

function initClipboard() {
    
    
    const clipboard = new Clipboard(copy.value)
    clipboard.on('success', () => {
    
    
        proxy.$toast({
    
    
            message: '复制成功'
        })
    })
    clipboard.on('error', () => {
    
    
        proxy.$toast({
    
    
            message: '复制失败'
        })
    })
}

</script>

4. The writing method in the template

    <span class='tagName'>订单号: >{
    
    {
    
    num}}</span>
    <span class='copy' ref="copy" :data-clipboard-text="num">复制</span>

insert image description here
It's such a happy solution! ! !

Guess you like

Origin blog.csdn.net/Smile_666666/article/details/124456546