简体   繁体   English

Angular 模块的 forRoot 中的运行时值

[英]Runtime value in forRoot of Angular Module

In my angular app there is this module called Ngx-Stripe在我的 angular 应用程序中,有一个名为Ngx-Stripe 的模块

I have defined it as documentation like following:我已将其定义为如下文档:

NgxStripeModule.forRoot('***your-stripe-publishable key***');

But the problem is I don't get this key on app bootstrap, I am not supposed to hardcode it in angular app.module or global like in index.html when I am using stripe withput any angular library.但问题是我在应用程序引导程序上没有得到这个键,当我使用带任何角度库的条带时,我不应该在 angular app.module或全局像index.html硬编码它。

I am getting this key on the api call after user proceeds to payment page.用户进入付款页面后,我在 api 调用中获得了此密钥。 How can I define this key in this scenario ?在这种情况下如何定义此键?

I wish it'd be straightforward, but the only way I was able to achieve it was something like:我希望它很简单,但我能够实现它的唯一方法是:

index.html (alternatively webpack-injected script), has to be placed before Angular's sources: index.html (或者 webpack-injected 脚本),必须放在 Angular 的源代码之前:

<script>
  var STRIPE_KEY = 'paste it here';
</script>

app.module.ts : app.module.ts

declare var STRIPE_KEY;

// ...
NgxStripeModule.forRoot(STRIPE_KEY);

The problem here is .forRoot() has to be statically-analyzed by Angular AoT compiler, so it can't accept what you want it to accept... How about setting the key after you got it via StripeService.changeKey(key: string) method?这里的问题是.forRoot()必须由 Angular AoT 编译器静态分析,所以它不能接受你想让它接受的东西......通过StripeService.changeKey(key: string)方法?

For lazy loading your Stripe config key you can create your own instance of StripeService using StripeFactory that is available from ngx-stripe.为了延迟加载你的 Stripe 配置键,你可以使用 ngx-stripe 提供的 StripeFactory 创建你自己的 StripeService 实例。

Check the documentation here for a full explanation:查看此处的文档以获取完整说明:

https://richnologies.gitbook.io/ngx-stripe/core-concepts/service-factory https://richnologies.gitbook.io/ngx-stripe/core-concepts/service-factory

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

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