Last active
May 19, 2019 18:45
-
-
Save notmike101/5000338ed28f8cdbb8278f0b40f6140d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
methods: { | |
/* | |
* Inject a script tag into the page | |
* Parameters: | |
* path: Path of JS file | |
* target (default = head): Element that script should be injected into | |
* Pass a querySelector or a node here | |
* isAsync (default= true): Apply the 'async' property to the output tag | |
* isDefer (default = true): Apply the 'defer' property to the output tag. | |
*/ | |
injectScript(path, target = 'head', isAsync = true, defer = true, id = path) { | |
const scriptTag = document.createElement("script"); | |
scriptTag.setAttribute('src', path); | |
scriptTag.async = isAsync; | |
scriptTag.defer = isDefer; | |
scriptTag.id = id; | |
if (target === 'head') { | |
document.getElementsByTagName('head')[0].appendChild(scriptTag); | |
} else { | |
target.appendChild(scriptTag); | |
} | |
} | |
}, | |
mounted() { | |
this.injectScript('https://bootswatch.com/_vendor/jquery/dist/jquery.min.js'); | |
this.injectScript('https://bootswatch.com/_vendor/popper.js/dist/umd/popper.min.js'); | |
this.injectScript ('https://bootswatch.com/_vendor/bootstrap/dist/js/bootstrap.min.js'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm getting this error:
Error in mounted hook: "ReferenceError: injectScript is not defined"