Search code examples
vuejs2clipboard

Copying to clipboard using vuejs2


What is wrong with the below code for copying to clipboard using vue and vue-clipboard?

<button v-clipboard:copy="message">Copy</button>

data: {
 message:"text to copy"
}

});

https://jsfiddle.net/29zhwcdf/2/

Data is not being copied to clipboard.


Solution

  • Make sure to use: Vue.use(vueClipboards), before instantiating your Vue application.

    Working example:

    Vue.use(vueClipboards); // Add this before 'new Vue({..})'
    var apptest = new Vue({
      el: "#apptest",
      data: {
        message: "text to copy"
      },
      methods: {
        handleSuccess(e) {
          console.log(e);
        },
        handleError(e) {
          console.log(e);
        },
      }
    });
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <script src="https://cdn.jsdelivr.net/npm/vue"></script>
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue-clipboards.min.js"></script>
    </head>
    
    <body>
      <div id="apptest">
        <button v-clipboard="message" @success="handleSuccess" @error="handleError">Copy</button>
      </div>
    
    </body>
    
    </html>