Security News
Namecheap Takes Down Polyfill.io Service Following Supply Chain Attack
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
vue-plugin-load-script
Advanced tools
Readme
A plugin for injecting remote scripts.
Compatible with Vue 2.
For Vue 3, see the vue3 branch.
# npm
npm install --save vue-plugin-load-script@^1.x.x
# yarn
yarn add vue-plugin-load-script@^1.x.x
// In main.js
import LoadScript from 'vue-plugin-load-script';
Vue.use(LoadScript);
// @/plugins/load-script.js
import Vue from 'vue';
import LoadScript from 'vue-plugin-load-script';
Vue.use(LoadScript);
// @/nuxt.config.js
//...
plugins: [
{ src: '@/plugins/load-script.js' },
],
//...
build: {
transpile: ['vue-plugin-load-script'],
},
//...
The build.transpile
option is required since this plugin is exported as an ES6 module.
// As a global method
Vue.loadScript("https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY")
.then(() => {
// Script is loaded, do something
})
.catch(() => {
// Failed to fetch script
});
// As an instance method inside a component
this.$loadScript("https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY")
.then(() => {
// Script is loaded, do something
})
.catch(() => {
// Failed to fetch script
});
Once loaded, the script can be accessed by their usual name in the global scope, as if the script were included in the page's <head>
.
If you are using a linter to check your code, it may warn on an undefined variable. You will need to instruct your linter to ignore this variable or function. See here for ESLint instructions. If you are unable to resolve this in your linter, try prefixing the loaded library's variable/function name with window.
.
:zap: New in 1.2!
If you'd like to remove (unload) the script at any point, then call the companion method $unloadScript
with the same URL.
// As a global method
Vue.unloadScript("https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY")
.then(() => {
// Script was unloaded successfully
})
.catch(() => {
// Script couldn't be found to unload; make sure it was loaded and that you passed the same URL
});
// As an instance method inside a component
this.$unloadScript("https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY")
.then(() => {
// Script was unloaded successfully
})
.catch(() => {
// Script couldn't be found to unload; make sure it was loaded and that you passed the same URL
});
In most situations, you can just call Vue.unloadScript
/this.$unloadScript
and ignore the returned promise.
FAQs
A Vue plugin for injecting remote scripts
The npm package vue-plugin-load-script receives a total of 19,697 weekly downloads. As such, vue-plugin-load-script popularity was classified as popular.
We found that vue-plugin-load-script demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.
Security News
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.