1. Use native methods

Generally used in the browser, so compatibility is not very good

I created the input directly using the native method, or you can simply append the input tag to the DOM and fetch its value.

copyUrl(){ let url = 'https://blog.csdn.net/linfng023/article/details/101014133'; let domInput = document.createElement('input'); domInput.value = url; document.body.appendChild(domInput); // Add the input node dominput.select (); // Select the object; document.execCommand("Copy"); $toast({message: 'Link copied successfully! `, duration: 2000 }); domInput.remove() },Copy the code

2. Use the plugin clipboard.js

1. Install clipboard.js using NPM or CNPM.

npm install clipboard --save
Copy the code

2. Import clipboard.js, either directly from mian.js (global use) or locally from any component you need to copy and paste (local use)

import clipboard from 'clipboard'; Prototype. Clipboard = clipboard;Copy the code

3. Then copy and paste

<button class="copyClass" data-clipboard-action="copy" data-clipboard-text="XXXXXX" @click="copyLink"> </button>Copy the code
copyLink() { let clipboardUrl = new this.clipboard(".copyClass"); Clipboardurl. on('success', function () {_this.$toast({message: 'link copy successful! `, duration: 2000 }); }); clipboardUrl.on('error', function () { console.log(clipboardUrl) }); }Copy the code