0

new to all these modern day frameworks I'm trying to get this javascript library to work in my Vue component: https://clipboardjs.com

I can see it is integrated into my public/js/app.js file when Mix runs - but I cannot seem to be able to reference it from within my .vue (component) file

Can anyone give me a step by step guide on how to get this to integrate into my project please:

resources/js/app.js file

 import './clipboard'; 

the clipboard.js file is located at:

resources/js/clipboard.js

Mix webpack.mix.js file:

mix.js("resources/js/app.js", "public/js")
 .js("resources/js/clipboard.js", "public/js")
 .vue()
 .postCss("resources/css/app.css", "public/css", [
   require("tailwindcss"),
 ]);

My Vue component (method section):

<script>
    export default {
        props: ['mediaDetails'],
        data() {
            return {
            }
        },
        methods: {
              copyPassword() {
            alert("copy");
            var password = document.getElementById('#password');
            alert('1');
             var clipboard = new ClipboardJS(password);
            alert('2');
          }
        }

the alert('2') never fires due to the clipboard assignemnet failing

1
  • You don't seems to import that npm package anywhere ? Commented Jun 28, 2022 at 8:20

1 Answer 1

1
  1. First, in your webpack.mix.js file, remove the line .js("resources/js/clipboard.js", "public/js")
  2. Install the clipboard package: npm install clipboard
  3. In your Vue component, import the package in order to use it:
<script>
   import ClipboardJS from 'clipboard';

   export default {
       props: ['mediaDetails'],
       data() {
           return {
           }
       },
       methods: {
             copyPassword() {
           alert("copy");
           var password = document.getElementById('#password');
           alert('1');
            var clipboard = new ClipboardJS(password);
           alert('2');
         }
       }
  1. BTW You have a typo error in document.getElementById('#password'), it should be document.getElementById('password')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @DVN-Anakin. I will give this a go and report back. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.