我创建了两个 Angular 库,其中一个库依赖另一个库。
需要使用 forRoot 方法配置依赖关系。我如何将配置数据从父库传递到它的依赖项?
例如,假设我们有TopLevelLib,它有OtherLib一个依赖项。需要使用 forRoot 向 OtherLib 传递一个配置对象。
最终用户的AppModule,导入到
@NgModule({
imports: [
TopLevelLib.forRoot(someConfigData)
],
declarations: [...],
exports: [...]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
TopLevelLib - 由最终用户导入到 AppModule
@NgModule({
imports: [
...
OtherLib.forRoot(*****what goes in here?*****)
],
declarations: [...],
exports: [...]
})
export class TopLevelLib {
static forRoot(config: ConfigObj): ModuleWithProviders {
return {
ngModule: SampleModule,
providers: [{ provide: SomeInjectionToken, useValue: config }]
};
}
}
Run Code Online (Sandbox Code Playgroud)
OtherLib - 由 TopLevelLib 导入
@NgModule({
imports: [...], …Run Code Online (Sandbox Code Playgroud)