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?
相关问题
- Axios OPTIONS instead of POST Request. Express Res
- Vue.js - set slot content programmatically
- Simple vue app doesn't show anything
- Getting Uncaught TypeError: …default is not a cons
- Vue.js computed property loses its reactivity when
相关文章
- Best way to dynamically change theme of my Vue.js
- vue-router how to persist navbar?
- How to Properly Use Vue Router beforeRouteEnter or
- Difference between nameFunction() {} and nameFunct
- Vue - best way to reuse methods
- setTimeout() not working called from vueJS method
- How to unbind an array copy in Vue.js
- Vue Cli 3 Local fonts not loading
I have downloaded some HTML template that comes with custom js files and jquery. I had to attach those js to my app. and continue with Vue.
Found this plugin, it's a clean way to add external scripts both via CDN and from static files https://www.npmjs.com/package/vue-plugin-load-script
Are you using one of the Webpack starter templates for vue (https://github.com/vuejs-templates/webpack)? It already comes set up with vue-loader (https://github.com/vuejs/vue-loader). If you're not using a starter template, you have to set up webpack and vue-loader.
You can then
import
your scripts to the relevant (single file) components. Before that, you have toexport
from your scripts what you want toimport
to your components.ES6 import:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import
- http://exploringjs.com/es6/ch_modules.html
~Edit~
You can import from these wrappers:
- https://github.com/matfish2/vue-stripe
- https://github.com/khoanguyen96/vue-paypal-checkout
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:Source: https://medium.com/@lassiuosukainen/how-to-include-a-script-tag-on-a-vue-component-fe10940af9e8
using webpack and vue loader you can do something like this
it waits for the external script to load before creating the component, so globar vars etc are available in the component
You can load the script you need with a promise based solution:
Please note that
this.$root
is a little hacky and you should use a vuex or eventHub solution for the global events instead.You would make the above into a component and use it wherever needed, it will only load the script when used.
You can use vue-loader and code your components in their own files (Single file components). This will allow you to include scripts and css on a component basis.