Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用外部JSON文件';定义NgModule时的值_Angular_Typescript - Fatal编程技术网

Angular 使用外部JSON文件';定义NgModule时的值

Angular 使用外部JSON文件';定义NgModule时的值,angular,typescript,Angular,Typescript,目前,我正在使用一种非常常见的解决方案将JSON文件的内容加载到Angular 5应用程序中,我可以将其注入到我的组件中。其工作原理如第节所述 我正在使用需要在app.module.ts文件中初始化的npm包 @NgModule({ declarations: [ AppComponent ], imports: [ /* .... */ [MsalModule.forRoot({ clientID:

目前,我正在使用一种非常常见的解决方案将JSON文件的内容加载到Angular 5应用程序中,我可以将其注入到我的组件中。其工作原理如第节所述

我正在使用需要在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 { }
@NgModule({
声明:[
应用组件
],
进口:[
/* .... */
[MsalModule.forRoot({
客户ID:“…”,
当局:“…”,
重定向URI:
})]
],
供应商:[
配置服务,
{
提供:应用程序初始化器,
useFactory:(config:ConfigService)=>()=>config.load(),
部门:[配置服务],
多:真的
}
]
引导:[AppComponent]
})
导出类AppModule{}
我使用Angular CLI,我知道我可以使用不同的环境文件,但JSON文件的内容在构建应用程序后可能会发生变化,我不能依赖于此


有没有一种方法可以在我需要的NgModule声明中立即使用JSON文件中的值?

可以找到一种很好的替代方法


这种JSON文件的方式可以作为JS对象加载,我们可以在声明NgModule之前访问它。

这种JSON文件的方式可以加载,我们可以在声明NgModule之前访问它

Json文件:

{
    "apiUrl":"http://localhost:52361/"
}
import Configuration from '../assets/config.json'; 

GridModule.forRoot(Configuration.apiUrl)
模块文件:

{
    "apiUrl":"http://localhost:52361/"
}
import Configuration from '../assets/config.json'; 

GridModule.forRoot(Configuration.apiUrl)

如果不从JSON文件中读取数据,window.location.origin将是完美的选择,但由于某些非常奇怪的原因,该解决方案只能在本地工作。一旦我将我的应用部署到Azure并尝试加载它,它将被实例化为一个“null”值,尽管console.log(window.location.origin)向我显示了我需要的正确URL。