我想知道是否有办法在ElementRef
没有构造函数注入的情况下获取实例Angular
?
为什么我需要它:
我有一个抽象的 BaseComponent 类,它被许多其他 Angular 组件类继承。我想避免ElementRef
通过调用注入所有子类super(elementRef)
,而是将其直接注入 BaseComponent 类 -
export abstract class BaseComponent {
private elementRef: ElementRef;
constructor() {
// Do we have some option like below?
// this.elementRef = Injector.get(ElementRef)
}
}
@Component({
selector: 'app-comp-one',
templateUrl: './comp-one.component.html',
styleUrls: ['./comp-one.component.css']
})
export class CompOne extends BaseComponent {
constructor() { // constructor(elementRef: ElementRef)
super(); // super(elementRef);
// here I can pass elementRef in super constructor call
// but that's what I don't want to do
}
}