Matin Rezaii
Matin Rezaii

Reputation: 134

Load another javascript file in VueJS component

how can i load Javascripts files in Vue js components ? (Best way)

Upvotes: 1

Views: 59

Answers (1)

A. El-zahaby
A. El-zahaby

Reputation: 1160

you can just create script element in the mounted method :

<script>
  export default {
    data: () => ({
     // ...data of your component
    }),
    mounted() {

      let script1 = document.createElement('script')
      script1.setAttribute('src', 'path/to/file.js')
      script1.async = true
      document.head.appendChild(script1)

    }
  }
</script>

Upvotes: 2

Related Questions