I'm using angular cli and angular 4. I've made some classes from which my components inherit behavivour! However during development their constructores grow and in the current phase I need to be able to allow other to use them whithout the need of knowing their constructors!
So after seeing Petr sugestion in dependency injection I tried, but had to make some adjustments due to errors the compiler was giving, I suppose due to diferences betwween angular 4 and angular 5!
This is my code:
Service Locator:
import {ReflectiveInjector} from "@angular/core";
export class ServiceLocator {
static injector: ReflectiveInjector;
}
module which imports the serviceLocator:
ServiceLocator.injector = ReflectiveInjector.resolveAndCreate(
Object.keys(services).map(key => ({
provide: services[key].provide,
useClass: services[key].provide,
deps: services[key].deps
}))
);
serviceList:
import {SysFwDatesService} from '../sysDates/sysFwDates.service';
import {MatSnackBar} from '@angular/material';
import {SysFwFormSpecBuilder} from '../uiValidation/SysFwFormSpecBuilder';
import {SysFwHttpApi} from '../http/SysFwHttpApi';
export const services: {[key: string]: {provide: any, deps: any[], useClass?: any}} = {
'SysFwDatesService': {
provide: SysFwDatesService,
deps: []
},
'MatSnackBar': {
provide: MatSnackBar,
deps: []
},
'SysFwFormSpecBuilder': {
provide: SysFwFormSpecBuilder,
deps: []
},
'SysFwHttpApi': {
provide: SysFwHttpApi,
deps: []
}
}
It seems to be working, however it seems to have lost the other providers and to be expecting all providers to be passed this way!
This is the error I'm getting:
No provider for HttpClient! (SysFwDatesService -> SysFwHttpApi -> HttpClient)
Do I need to put everything in the servicesList? What am I doing wrong?
Before I use the injector it all worked fine!
Thanks for your help!