Angular 如何在嵌套的forRoot模块中使用配置数据

Angular 如何在嵌套的forRoot模块中使用配置数据,angular,Angular,我在我的项目中为多个应用程序使用了一个核心模块。每个应用程序都应该有不同的api url。核心模块导入包括许多外部lib,其中许多需要配置对象才能导入。例如,需要一个基本api url,因此,如何通过核心模块配置对象将每个应用程序的api url更改为外部模块 在我的示例中,有两点没有显示(为了使问题尽可能简单),但确实需要了解: 我正在使用配置服务在核心模块的许多地方(主要是在核心的服务中)注入配置对象 从JSON文件加载的config对象,以获得在生产中更改配置的选项 应用程序主模块之一

我在我的项目中为多个应用程序使用了一个核心模块。每个应用程序都应该有不同的api url。核心模块导入包括许多外部lib,其中许多需要配置对象才能导入。例如,需要一个基本api url,因此,如何通过核心模块配置对象将每个应用程序的api url更改为外部模块

在我的示例中,有两点没有显示(为了使问题尽可能简单),但确实需要了解:

  • 我正在使用配置服务在核心模块的许多地方(主要是在核心的服务中)注入配置对象
  • 从JSON文件加载的config对象,以获得在生产中更改配置的选项
应用程序主模块之一:

import {NgModule} from '@angular/core';
import {CoreModule} from '@app-common/core';

import {AppComponent} from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [
    CoreModule.forRoot({
      url: 'API-URL' <= Api url should to pass here.
    })
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
}
import {NgxJsonapiModule} from 'ngx-jsonapi';
import {throwIfAlreadyLoaded} from '@app-common/core/module-import-guards';

@NgModule({
  imports: [
    NgxJsonapiModule.forRoot({
      url: 'API-URL' <= Api url should used from the config object that pass into the forRoot method of the core module.
    })
  ]
})
export class CoreModule {
  static forRoot(config: {url: string}): ModuleWithProviders {
    return {
      ngModule: ProteanCoreModule,
      providers: []
    };
  }
  constructor(@Optional() @SkipSelf() parentModule: CoreModule, router: Router) {
    throwIfAlreadyLoaded(parentModule, 'CoreModule');
  }
}
从'@angular/core'导入{NgModule};
从'@app common/core'导入{CoreModule};
从“./app.component”导入{AppComponent};
@NGD模块({
声明:[AppComponent],
进口:[
CoreModule.forRoot({
url:'API-url'