我有一个共享模块,用于导入和配置另一个模块。
我想要做的是隐藏共享模块与根模块的依赖关系,但允许根模块通过配置共享模块来配置它。例如:
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(static forRoot)似乎无法做到这一点,因为它不会暴露导入。有什么想法吗?
答案 0 :(得分:0)
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) {