我想用一个
工厂供应商
它根据当前的URI选择服务实现。
此外,其中一个实现需要对路由参数作出反应(定义如下
route/:parameter
)
如下图所示,我订阅的更改很不幸,我无法以这种方式访问参数。在主路由器出口中实例化的组件可以访问它们,但FactoryProvider不能。
有什么我可能错过的吗?
模块代码(为提高可读性而缩写)
@NgModule({
declarations: [],
imports: [],
exports: [],
providers: [XServiceProvider],
entryComponents: []})
工厂供应商
export const XServiceProvider = {
provide: XService,
useFactory: XServiceFactory,
deps: [Http, HttpService, ConfigurationService, DatePipe, Router, ActivatedRoute]
};
const XServiceFactory = (http: Http,
httpService: HttpService,
configuration: ConfigurationService,
datePipe: DatePipe,
router: Router,
activatedRoute: ActivatedRoute) => {
const useAlternateImpplementation= router.url.startsWith('/mg/');
if (useAlternateImpplementation === true) {
const service = new XyService(http, httpService, configuration, datePipe);
for (const child of activatedRoute.root.children) {
if (child.outlet === PRIMARY_OUTLET) {
child.url.subscribe(params => {
service.setParam(params[parameter]);
});
}
}
return service;
}
return new XService(http, httpService, configuration, datePipe);
};