简体   繁体   English

如何在Angular 5中的app.module中读取appsettings文件?

[英]How to read appsettings file in app.module in Angular 5?

Hi I am developing web application in Angular 5. I am trying to read appsettings.json and use the value in app.module.ts. 嗨,我正在Angular 5中开发Web应用程序。我试图读取appsettings.json并使用app.module.ts中的值。 In app.module.ts inside imports I have following line of code. 在导入内部的app.module.ts中,我有以下代码行。

 AppInsightsModule.forRoot('dfd77fnd-3ba9-43e6-a90f-3e762444938b')

In the above code value is hard coded. 在上面的代码中,值是硬编码的。 I have below tag in appsettings.json. 我在appsettings.json中有以下标记。

 "ApplicationInsights": {
    "InstrumentationKey": "dfd77fnd-3ba9-43e6-a90f-3e762444938b"
  }

I want to read from appsettings.json. 我想从appsettings.json阅读。 Below the app.module.ts I can read it. 在app.module.ts下面,我可以阅读它。 I have created service and inject it. 我创建了服务并将其注入。 App.module.ts the root file in Angular. App.module.ts在Angular中的根文件。 Here how can i take the value from appsettings.json? 在这里,我如何从appsettings.json中获取值? Can someone help me to identify the solution for this? 有人可以帮助我确定解决方案吗? Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。

For loading the configuration data before application load you can use angular provided APP_INITIALIZER. 要在应用程序加载之前加载配置数据,可以使用提供的angular_APP_INITIALIZER。

Details at :- https://devblog.dymel.pl/2017/10/17/angular-preload/ https://www.intertech.com/Blog/angular-4-tutorial-run-code-during-app-initialization/ 有关详细信息,请访问: -https : //devblog.dymel.pl/2017/10/17/angular-preload/ https://www.intertech.com/Blog/angular-4-tutorial-run-code-during-app-初始化/

 providers: [
    ConfigProvider, 
    { provide: APP_INITIALIZER, useFactory: configProviderFactory, deps: [ConfigProvider], multi: true }
  ],
  bootstrap: [AppComponent]

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

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