Angular 角度:从组件获取nativeElement

Angular 角度:从组件获取nativeElement,angular,Angular,有没有一种简单的方法可以从组件实例获取nativeElement(或ElementRef)?我从视图中引用一个组件,如下所示: @ViewChild(MyComponent) myComponent: MyComponent; 我知道我可以得到ElementRef,而不是像这样得到MyComponent: @ViewChild('my-component', { read: ElementRef}) myComponent: ElementRef; 如果我只需要一个ElementRef,这很

有没有一种简单的方法可以从组件实例获取nativeElement(或ElementRef)?我从视图中引用一个组件,如下所示:

@ViewChild(MyComponent) myComponent: MyComponent;
我知道我可以得到ElementRef,而不是像这样得到MyComponent:

@ViewChild('my-component', { read: ElementRef}) myComponent: ElementRef;

如果我只需要一个ElementRef,这很好,但我实际上需要MyComponent的实例,但在稍后的某个时候,我需要将其向下转换为ElementRef,以便访问nativeElement。

从Angular 9开始,我不知道如何从组件引用获取本机元素。但是,可以同时获得两者

@ViewChild(MyComponent)组件:MyComponent;
@ViewChild(MyComponent,{read:ElementRef})ElementRef:ElementRef;