I'm new to Angular. My goal is to have a BaseDetailComponent which can be extended by "detail" components (those which show a single data object's fields). Since the parent class' constructor will receive a service instance and other params, I can't use normal dependency injection in the constructor. Therefore, I need to manually inject the application injector in BaseDetailComponent. I tried the code below. There is an InjectorManager, which has its injector set by the global AppModule. Then BaseDetailComponent retrieves InjectorManager's injector and uses it to get the app's ActivatedRoute instance. But, for some reason, activatedRoute.snapshot.params.id is undefined. And this is my issue. I need to access route params on a class where ordinary constructor dependency injection is unavailable.
export class InjectorManager {
private static _injector: Injector;
static get injector(): Injector {
return this._injector;
}
static set injector(value: Injector) {
this._injector = value;
}
}
export class AppModule {
constructor(
injector: Injector
) {
InjectorManager.injector = injector;
}
}
@Component({template: ''})
export class BaseDetailComponent {
protected activatedRoute: ActivatedRoute;
constructor(
private service: BaseService<DtoType>
) {
this.activatedRoute = InjectorManager.injector.get(ActivatedRoute);
}
ngOnInit() {
this.service.findById(this.activatedRoute.snapshot.params.id)
.subscribe(...);
}
}