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

时间:2018-07-19 05:53:56

标签: json angular appsettings

嗨,我正在Angular 5中开发Web应用程序。我试图读取appsettings.json并使用app.module.ts中的值。在导入内部的app.module.ts中,我有以下代码行。

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

在上面的代码中,值是硬编码的。我在appsettings.json中有以下标记。

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

我想从appsettings.json中读取。在app.module.ts下面,我可以阅读它。我创建了服务并将其注入。 App.module.ts在Angular中的根文件。在这里,我如何从appsettings.json中获取值?有人可以帮助我确定解决方案吗?任何帮助,将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

要在应用程序加载之前加载配置数据,可以使用提供的角度APP_INITIALIZER。

详细信息,位于:-https://devblog.dymel.pl/2017/10/17/angular-preload/               https://www.intertech.com/Blog/angular-4-tutorial-run-code-during-app-initialization/

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