我有一个
TableComponent
它需要一个抽象服务来执行一些异步调用。该模块看起来像:
@NgModule({
imports: [
CommonModule,
],
declarations: [
TableComponent
],
exports: [
TableComponent
]
})
export class TableModule {
static forRoot(tableService: Type<TableService>): ModuleWithProviders {
return {
ngModule: TableModule,
providers: [
{ provide: TableService, useClass: tableService }
]
};
}
}
我必须进一步模块化:员工模块和本地化模块。两者看起来都是:
@NgModule({
imports: [
CommonModule,
TableModule.forRoot(EmployeeTableService), <---- Injected EmployeeTableService
],
declarations: [
EmployeeComponent,
ModalEmployeeComponent
],
providers: [
EmployeeResource,
EmployeeRepository,
EmployeeTableService
]
})
export class EmployeeModule {
}
@NgModule({
imports: [
CommonModule,
TableModule.forRoot(LocalizationTableService),
],
declarations: [
LocalizationComponent
],
providers: [
LocalizationResource,
LocalizationRepository,
LocalizationTableService
],
})
export class LocalizationModule {
}
现在,我将这两个模块导入到AppModule中
订单。
。所以它看起来像:
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
HttpClientModule,
AppRoutes,
LocalizationModule,
EmployeeModule
],
providers: [
],
bootstrap: [AppComponent]
})
export class AppModule {
}
这里的问题是命令。取决于服务最后注入到哪个模块(员工或本地化)
表组件
,它将抽象作为构造函数的参数。即,如果订单与上述相同
EmployeeTableService
注射。否则
LocalizationTableService
注射-为什么以及如何改变它
表组件
是否总是采用正确的抽象?
这个
表组件
看起来像:
@Component({
selector: 'sp-table[configuration]',
templateUrl: './TableComponent.html',
styleUrls: ['./TableComponent.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class TableComponent implements OnInit {
constructor(
public readonly dialog: MatDialog,
private readonly tableService: TableService,
private readonly changeDetectorRef: ChangeDetectorRef
) {
super();
}
EmployeeTableService示例:
@Injectable()
export class EmployeeTableService extends TableService {
constructor(
private readonly employeeResource: EmployeeResource
) {
super();
}
}