【问题标题】:Configure Module in Shared Module from Root从根目录配置共享模块中的模块
【发布时间】:2018-05-23 12:14:51
【问题描述】:

我有一个共享模块,它可以导入和配置另一个模块。

我想做的是隐藏共享模块对根模块的依赖关系,但允许根模块通过配置共享模块来配置它。例如:

SharedModule.ts:

 @NgModule({
  imports: [
    //want to configure this from root module without importing into root module
    ConfigModule.forRoot({
      provide: ConfigLoader,
      useFactory: (configFactory),
      deps: [HttpClient]
    })
  ],
  declarations: [MyComponent],
  exports: [MyComponent],
  providers: [HttpClient]
})
export class SharedModule{ }

AppModule.ts:

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    AppRoutingModule,
    SharedModule, //Want to configure ConfigModule from here
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

这似乎通过实现 ModuleWithProviders(静态 forRoot)是不可能的,因为它不公开导入。有什么想法吗?

【问题讨论】:

    标签: angular typescript


    【解决方案1】:
    export class SampleModule {
      static forRoot(config: CustomConfig): ModuleWithProviders {
        // User config get logged here
        console.log(config);
        return {
          ngModule: SampleModule,
          providers: [SampleService, {provide: 'config', useValue: config}]
        };
      }
    }
    @Injectable()
    export class SampleService {
    
      foo: any;
    
      constructor(@Inject('config') private config:any) {
    

    【讨论】:

    • 那是配置 CustomConfig,而不是 ConfigModule。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多