vue js copy text to clipboard

  1. First, import the Clipboard API from the Vue.js library by adding the following import statement at the beginning of your Vue component:
import { Clipboard } from 'vuetify';
  1. Next, create a method in your Vue component to handle the copying of text to the clipboard. You can name this method "copyToClipboard" and use the following code:
methods: {
  copyToClipboard(text) {
    Clipboard.writeText(text)
      .then(() => {
        console.log('Text copied to clipboard');
      })
      .catch(err => {
        console.error('Failed to copy text to clipboard: ', err);
      });
  }
}
  1. Now, you can use this method in your Vue template to trigger the copy action. For example, you can create a button that calls the "copyToClipboard" method with the text you want to copy:
<button @click="copyToClipboard('This text will be copied to the clipboard')">Copy Text</button>
  1. Finally, you can test the functionality by clicking the button and verifying that the specified text is copied to the clipboard. If successful, you should see the "Text copied to clipboard" message in the console.

This process allows you to integrate text copying functionality into your Vue.js application using the Vue Clipboard API.