I have a few components; I'm using Injector in constructor for encapsulation
import { Component, Injector, OnInit } from '@angular/core';
@Component({
selector: 'app-base',
templateUrl: './base.component.html',
styleUrls: ['./base.component.css'],
})
export class BaseComponent implements OnInit {
some = '';
constructor(injector: Injector) {
this.some = injector.get(this.some);
}
ngOnInit(): void {}
}
I'm using BaseComponent in other Component
import { BaseComponent } from '../base/base.component';
@Component({
selector: 'app-base-state',
templateUrl: './base-state.component.html',
styleUrls: ['./base-state.component.css'],
})
export class BaseStateComponent extends BaseComponent implements OnInit {
constructor(injector: Injector) {
super(injector);
}
ngOnInit(): void {}
}
BaseStateComponent I'm going to use in others component; Question is: Is there any way, to make injector in BaseComponent or BaseSateComponent Optional; I have a case, when I need a component, but I don't need an injector of it;
I know about feature
constructor(@Optional(), @Self() etc...);
But truly to say, I can't understand how it work's; I will be grateful if somebody can explain it Decorators;
@Optional(),constructor(@Optional() injector: Injector)?, the optional decorator marks the injector parameter in the constructor as an optional dependency, which is exactly what you want right?