I’ve to use two external scripts for the payment gateways.
Right now both are put in the index.html
file.
However, I don’t want to load these files at the beginning itself.
The payment gateway is needed only in when user open a specific component (using router-view
).
Is there anyway to achieve this?
Thanks.
Advertisement
Answer
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:
JavaScript
x
20
20
1
<template>
2
your HTML .
3
</template>
4
5
<script>
6
export default {
7
data: () => ({
8
data of your component
9
}),
10
mounted() {
11
let recaptchaScript = document.createElement('script')
12
recaptchaScript.setAttribute('src', 'https://www.google.com/recaptcha/api.js')
13
document.head.appendChild(recaptchaScript)
14
},
15
methods: {
16
methods of your component
17
}
18
}
19
</script>
20
Source: https://medium.com/@lassiuosukainen/how-to-include-a-script-tag-on-a-vue-component-fe10940af9e8