How to add external JS scripts to VueJS Components?
A simple and effective way to solve this, it's by adding your external script into the vue mounted()
of your component. I will illustrate you with the Google Recaptcha script:
<template>
.... your HTML
</template>
<script>
export default {
data: () => ({
......data of your component
}),
mounted() {
let recaptchaScript = document.createElement('script')
recaptchaScript.setAttribute('src', 'https://www.google.com/recaptcha/api.js')
document.head.appendChild(recaptchaScript)
},
methods: {
......methods of your component
}
}
</script>
Source: https://medium.com/@lassiuosukainen/how-to-include-a-script-tag-on-a-vue-component-fe10940af9e8
How to add external JS scripts to VueJS Components?
A simple and effective way to solve this, it's by adding your external script into the vue mounted()
of your component. I will illustrate you with the Google Recaptcha script:
<template>
.... your HTML
</template>
<script>
export default {
data: () => ({
......data of your component
}),
mounted() {
let recaptchaScript = document.createElement('script')
recaptchaScript.setAttribute('src', 'https://www.google.com/recaptcha/api.js')
document.head.appendChild(recaptchaScript)
},
methods: {
......methods of your component
}
}
</script>
Source: https://medium.com/@lassiuosukainen/how-to-include-a-script-tag-on-a-vue-component-fe10940af9e8
Importing javascript file for use within vue component
Include an external JavaScript file
Try including your (external) JavaScript into the mounted hook of your Vue component.
<script>
export default {
mounted() {
const plugin = document.createElement("script");
plugin.setAttribute(
"src",
"//api.myplugincom/widget/mykey.js"
);
plugin.async = true;
document.head.appendChild(plugin);
}
};
</script>
Reference: How to include a tag on a Vue component
Import a local JavaScript file
In the case that you would like to import a local JavaScript in your Vue component, you can import it this way:
MyComponent.vue
<script>
import * as mykey from '../assets/js/mykey.js'
export default {
data() {
return {
message: `Hello ${mykey.MY_CONST}!` // Hello Vue.js!
}
}
}
</script>
Suppose your project structure looks like:
src
- assets
- js
- mykey.js
- components
MyComponent.vue
And you can export variables or functions in mykey.js:
export let myVariable = {};
export const MY_CONST = 'Vue.js';
export function myFoo(a, b) {
return a + b;
}
Note: checked with Vue.js version 2.6.10
Related Topics
How Does Facebook Disable the Browser's Integrated Developer Tools
How to Fix Error: Listen Eaddrinuse While Using Nodejs
How to Determine User's Locale Within Browser
How to Parse JSON Using Node.Js
Working with Select Using Angularjs's Ng-Options
JavaScript Setinterval and 'This' Solution
Using Jquery's Ajax Method to Retrieve Images as a Blob
Convert Utc Epoch to Local Date
Is Safari on iOS 6 Caching $.Ajax Results
Adding Click Event Listener to Elements with the Same Class
What's the Difference Between Window.Location and Document.Location in JavaScript
Check If a Variable Is of Function Type
Passing Custom Props to Router Component in React-Router V4
How to Use Meteor Methods Inside of a Template Helper
Using JavaScript's Atob to Decode Base64 Doesn't Properly Decode Utf-8 Strings
Why Do We Use Spreadsheetapp.Flush();
How to Get a Dom Element from a Jquery Selector
Javascript: How to Join/Combine Two Arrays to Concatenate into One Array