How to add external JS scripts to VueJS Components

How to add external JS scripts to VueJS Components

Asked on November 22, 2018 in vue.js.
Add Comment


  • 1 Answer(s)

    Very easy path to solve this problem is that you should add your external script inside vue mounted() of element. Here, I show this to you using Google recaptcha script:

    <template>
      .... your HTML
    </template>
     
    <script>
    export default {
      data: () => ({
        ......data of your component
      }),
      mounted() {
        let recaptchaScript = document.createElement('script')
        recaptchaScript.setAttribute('src', 'https://www.google.com/recaptcha/api.js')
        document.head.appendChild(recaptchaScript)
      },
      methods: {
        ......methods of your component
      }
    }
    </script>
    
    Answered on November 22, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.