目前,我正在使用一个非常常见的解决方案将JSON文件的内容加载到我的Range5应用程序中,我可以将该应用程序插入到组件中。它的工作原理就像这里在运行时配置小节中描述的那样。
我使用的是需要在我的app.module.ts文件中初始化的npm包
@NgModule({
declarations: [
AppComponent
],
imports: [
/* .... */
[MsalModule.forRoot({
clientID: "...",
authority: "...",
redirectUri: <I need the value here from the JSON file>
})]
],
providers: [
ConfigService,
{
provide: APP_INITIALIZER,
useFactory: (config: ConfigService) => () => config.load(),
deps: [ConfigService],
multi: true
}
]
bootstrap: [AppComponent]
})
export class AppModule { }
我使用Angular,我知道我可以使用不同的环境文件,但是在构建应用程序之后,JSON文件的内容可能会改变,我不能依赖它。
是否有一种方法可以在NgModule声明中立即使用JSON文件中的值,我需要它?
发布于 2018-09-14 04:55:27
一个很好的替代方案可以找到在这个StackOverflow问题中
通过这种方式可以将JSON文件作为JS对象加载,并且我们可以在声明NgModule之前访问它。
发布于 2020-01-09 02:04:50
通过这种方式可以加载JSON文件,并且我们可以在声明NgModule之前访问它。
Json文件:
{
"apiUrl":"http://localhost:52361/"
}
模块文件:
import Configuration from '../assets/config.json';
GridModule.forRoot(Configuration.apiUrl)
https://stackoverflow.com/questions/52284255
复制相似问题