0

Hi I have a code like this in VueJs:

methods: {
   copyUrl(data) {
      console.log(data);
      data.select();
      document.execCommand("copy");
   }
}

My button is:

 <button v-on:click="copyUrl('1234')" class="btn btn-primary btn-circle btn-sm">
     <i class="fas fa-link"></i>
 </button>

I wonder how can I copy the 1234? I mean I need to create a url copy button.

Thanks

Jis
  • 35
  • 1
  • 7

1 Answers1

0

No need to pass data to the function, try something like below

methods: {
  copyUrl() {
   var cpLink = window.location.href;
   cpLink.select();
   try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    console.log('Copy command was ' + msg);
   } catch (err) {
     console.log('Oops, unable to copy');
   }
   event.preventDefault; 
 }
}
Amaarockz
  • 4,348
  • 2
  • 9
  • 27