vue-plugin-load-script
A Vue plugin for injecting remote scripts.
Compatible with Vue 3.
For Vue 2, see the master branch.
Install
npm install --save vue-plugin-load-script@^2.x.x
yarn add vue-plugin-load-script@^2.x.x
If you're using the Options API:
import { createApp } from "vue";
import LoadScript from "vue-plugin-load-script";
const app = createApp(App);
app.use(LoadScript);
app.mount("#app");
Use
import { loadScript } from "vue-plugin-load-script";
loadScript("https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY")
.then(() => {
})
.catch(() => {
});
this.$loadScript("https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY")
.then(() => {
})
.catch(() => {
});
If you'd like to remove (unload) the script at any point, then call the companion method unloadScript
/ this.$unloadScript
with the same URL.
import { unloadScript } from "vue-plugin-load-script";
unloadScript("https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY")
.then(() => {
})
.catch(() => {
});
this.$unloadScript("https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY")
.then(() => {
})
.catch(() => {
});
In most situations, you can just call unloadScript
/ this.$unloadScript
and ignore the returned promise.