Могу ли я сделать это:Угловое 2: инъекции собственности вместо инъекции конструктора
export class BaseComponent {
protected config: IConfig;
@Inject(AppConfig) protected appConfig: AppConfig;
constructor()
{
this.config = this.appConfig.getConfig();
}
вместо этого:
export class BaseComponent {
config: IConfig;
constructor(
private appConfig: AppConfig,
)
{
this.config = appConfig.getConfig();
}
Цель состоит в том, чтобы упростить конструктор подписи, поэтому все дочерние компоненты не необходимо указать appConfig в своем конструкторе. Таким образом, компоненты, которые наследует от BaseComponent, чтобы выглядеть следующим образом:
@Component({
selector: 'sport-templates',
templateUrl: 'templates.component.html',
styleUrls: [ 'templates.component.scss' ],
encapsulation: ViewEncapsulation.None
})
export class SportTemplates extends BaseComponent implements OnInit {
constructor() {
super();
}
вместо так:
@Component({
selector: 'sport-templates',
templateUrl: 'templates.component.html',
styleUrls: [ 'templates.component.scss' ],
encapsulation: ViewEncapsulation.None
})
export class SportTemplates extends BaseComponent implements OnInit {
constructor(appConfig: AppConfig) {
super(appConfig);
}
Вы нашли самое лучшее разрешение для инъекций конструктора? – PaladiN
Я нашел ответ на свой вопрос здесь: https://stackoverflow.com/questions/42461852/angular-2-inject-service-manually Спасибо. –