如何使用 ViewChild 获取整个 HTML 元素?
我努力了:
@ViewChild("mapContainer", { static: false }) mapContainer: HTMLElement;
其中'd'是参考:
<div #mapContainer></div>
像这样试试
@ViewChild('mapContainer', { static: true }) map: ElementRef;
ngOnInit() {
console.log(this.map.nativeElement);
}
在这里您可以阅读更多关于 ViewChild 的信息。
ElementRef与ViewChild,一起使用
import { ElementRef, ViewChild } from '@angular/core';
@ViewChild('mapContainer', {static: false})
private el: ElementRef;
ngOnInit(){
const element = this.el.nativeElement;
}