在我的 Angular 应用程序中有一个名为 Ngx-Stripe 的模块
我将其定义为如下文档:
NgxStripeModule.forRoot('***your-stripe-publishable key***');
但问题是我没有在应用程序 Bootstrap 中获得此 key ,我不应该将其硬编码在 Angular app.module
或全局的 index.html
中> 当我使用 stripe withput any angular 库时。
在用户进入支付页面后,我在 api 调用中获得了这个 key 。在这种情况下如何定义此 key ?
最佳答案
我希望它是直截了当的,但我能够实现它的唯一方法是:
index.html(或者 webpack 注入(inject)的脚本),必须放在 Angular 的源代码之前:
<script>
var STRIPE_KEY = 'paste it here';
</script>
app.module.ts:
declare var STRIPE_KEY;
// ...
NgxStripeModule.forRoot(STRIPE_KEY);
这里的问题是 .forRoot()
必须由 Angular AoT 编译器进行静态分析,所以它不能接受你想让它接受的东西......如何设置 key 之后你是通过 StripeService.changeKey(key: string)
方法得到的吗?
关于javascript - Angular 模块的 forRoot 中的运行时值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48502608/