我无法在Require.js中加载Stripe.js。我的设置看起来像这样
requirejs.config({
paths: {
'stripe': 'https://js.stripe.com/v3/?noext'
},
shim: {
'stripe': {
exports: 'stripe'
}
}
});
这确实有效,也就是说,我可以在dom中看到script标记,但是当我需要它时,它是undefined
。任何想法在这里会发生什么?
答案 0 :(得分:2)
条带导出的全局变量为Stripe
,其大写字母为“ S”。 exports
需要完全匹配全球出口,即大小写。
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>
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>