【发布时间】:2016-09-06 08:36:57
【问题描述】:
我有一个名为 CalculationService 的服务,声明为 @Injectable()。该服务使用了很多其他注入的类,每个注入类也声明为@Injectable(),并通过@Inject() 提供给CalculationService 的构造函数。
现在当我想在我的应用程序中使用服务时,AppComponent 看起来像这样(为了方便,我省略了 Typescript 导入):
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
RouterModule.forRoot(routeConfig),
PlannerModule
],
providers: [
InputService,
Bausparen,
BausparenInput,
Girokonto,
GirokontoInput,
Immobilien,
ImmobilienInput,
Lebensversicherung,
LebensversicherungInput,
Rentenversicherung,
RentenversicherungInput,
Tagesgeld,
TagesgeldInput,
Termingeld,
TermingeldInput,
Wertpapiere,
WertpapiereInput
],
bootstrap: [AppComponent]
})
export class AppModule {
}
我不需要直接访问 app 模块中声明的所有提供程序。唯一需要的类是 CalculationService,但我不知道如何在 Service 中声明提供程序。
有没有办法将服务重构为一个模块,即在 CalculationService 中声明提供程序?还是我必须摆脱所有@Injectable()s(Bausparen 等)并在计算服务中创建对象,这样就不需要提供程序了?
【问题讨论】: