繁体   English   中英

如何在 AppNgModule forRoot() 中添加动态值?

[英]How to add dynamic value in AppNgModule forRoot()?

我已经搜索了很多,但还没有找到任何解决方案。

NgxStripeModule.forRoot('publishable_key'),

我需要这个“publishable_key”应该是动态的,它将来自 REST API。

有什么选择吗?

谢谢你。

您首先必须使用forRoot方法将模块注册为空白字符串NgxStripeModule.forRoot('') 稍后使用APP_INITIALIZER进行 API 调用,接收设置,提取publishable_key并使用stripeService.changeKey('publishable_key') method. 此过程将在应用程序启动之前发生。 因此,无需担心选择正确的设置。

function initializeApp(http: HttpClient, stripeService: StripeService): Observable<any> {
  return http.get('api/url').pipe(
    tap((data) => stripeService.changeKey(data.publishable_key)) // or .setKey(...)
  );
}
@NgModule({
 imports: [
   ...,
   NgxStripeModule.forRoot(''),
 ],
 declarations: [...],
 bootstrap: [AppComponent],
 providers: [
  ...,
  {
   provide: APP_INITIALIZER,
   useFactory: initializeApp,
   multi: true,
   deps: [HttpClient, StripeService]
  }
 ]
})
export class AppModule {}

暂无
暂无

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

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