简体   繁体   中英

Load Stripe.js with Require.js

I'm having trouble loading Stripe.js with Require.js. My setup looks a bit like this

requirejs.config({
  paths: {
    'stripe': 'https://js.stripe.com/v3/?noext'
  },
  shim: {
    'stripe': {
      exports: 'stripe'
    }
  }
});

This actually does work, that is, I can see the script tag in the dom but when I require it it's undefined . Any ideas what could be happening here?

The global that stripe exports is Stripe with an uppercase "S". The exports needs to match the global export exactly , meaning case.

This works:

 requirejs.config({ paths: { 'stripe': 'https://js.stripe.com/v3/?noext' }, shim: { 'stripe': { exports: 'Stripe' // Uppercase } } }); require(['stripe'], function(s) { // Based on code from https://stripe.com/docs/stripe-js/elements/quickstart const ss = s('pk_test_g6do5S237ekq10r65BnxO6S0'); const elements = ss.elements(); const card = elements.create('card'); card.mount('#card-element'); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.js"></script> <div id="card-element"></div> 

This doesn't:

 requirejs.config({ paths: { 'stripe': 'https://js.stripe.com/v3/?noext' }, shim: { 'stripe': { exports: 'stripe' // Lowercase } } }); require(['stripe'], function(s) { console.log(s); // undefined }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM