close

[Solved] How to add external JS scripts to VueJS Components?

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error How to add external JS scripts to VueJS Components? in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How to add external JS scripts to VueJS Components Error Occurs?

Today I get the following error How to add external JS scripts to VueJS Components? in javascript.

How To Solve add external JS scripts to VueJS Components Error ?

  1. How To Solve add external JS scripts to VueJS Components Error ?

    To Solve add external JS scripts to VueJS Components Error You can use vue-loader and code your components in their own files (Single file components). This will allow you to include scripts and css on a component basis.

  2. How to add external JS scripts to VueJS Components?

    To Solve add external JS scripts to VueJS Components Error You can use vue-loader and code your components in their own files (Single file components). This will allow you to include scripts and css on a component basis.

Solution 1

using webpack and vue loader you can do something like this

it waits for the external script to load before creating the component, so globar vars etc are available in the component

components: {
 SomeComponent: () => {
  return new Promise((resolve, reject) => {
   let script = document.createElement('script')
   script.onload = () => {
    resolve(import(someComponent))
   }
   script.async = true
   script.src = 'https://maps.googleapis.com/maps/api/js?key=APIKEY&libraries=places'
   document.head.appendChild(script)
  })
 }
},

Solution 2

You can use vue-loader and code your components in their own files (Single file components). This will allow you to include scripts and css on a component basis.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment