Load Stripe.js with Require.js

一笑奈何 提交于 2021-02-10 18:00:15

问题


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?


回答1:


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>


来源:https://stackoverflow.com/questions/50976831/load-stripe-js-with-require-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!