我正在尝试扩展Angular库,但是我的库需要对其中一项服务进行特定设置才能正常工作。我希望可以以某种方式使用装饰器模式(而不是TypeScript装饰器)通过工厂重新提供服务。这将涉及将先前声明的服务注入工厂,并修改现有实现或返回使用原始版本的替代版本。看来我的工厂方法没有被调用。
export function decoratedTranslateServiceFactory(tranlateService: TranslateService) {
// either make changes to translateService or return an
// alternate version that uses the original one
// (i.e. new MyTranslateService(translateService))
}
@NgModule({
imports: [TranslateModule],providers: [
{
provide: TranslateService,useFactory: decoratedTranslateServiceFactory,deps: [TranslateService] // <-- Same type as provided
}
]
})
export class MyTranslateModule {}
@NgModule({
imports: [TranslateModule,MyTranslateModule]
})
export class AppModule {}
我希望TranslateService的修改版本(由我的模块提供的工厂提供)将是提供给应用程序其余部分的版本。我希望可以使用这种方法或类似的方法来扩展或包装现有服务。这有可能吗?
谢谢您的建议。
编辑:添加一个StackBlitz。