简体   繁体   English

使用Require.js加载Stripe.js

[英]Load Stripe.js with Require.js

I'm having trouble loading Stripe.js with Require.js. 我在使用Require.js加载Stripe.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 . 这实际上是有效的,也就是说,我可以在dom中看到script标记,但是当我需要它时,它是undefined Any ideas what could be happening here? 有什么想法会在这里发生吗?

The global that stripe exports is Stripe with an uppercase "S". 条带导出的全局变量是带有大写“ S”的Stripe带。 The exports needs to match the global export exactly , meaning case. exports需要精确匹配全球出口,这就是案例。

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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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