Nodular Pudding Multiverse
Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

vue-plugin-load-script

1.2.0 • Public • Published

vue-plugin-load-script license

A Vue plugin for injecting remote scripts.

Install

# npm
npm install --save-dev vue-plugin-load-script
# yarn
yarn add --dev vue-plugin-load-script

Use

  // In main.js
  import LoadScript from 'vue-plugin-load-script';
 
  Vue.use(LoadScript);
  // 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
    });

⚡️ 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.

Keywords

none

install

npm i vue-plugin-load-script

Downloadsweekly downloads

657

version

1.2.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability