-2

如何使用 ViewChild 获取整个 HTML 元素?

我努力了:

   @ViewChild("mapContainer", { static: false }) mapContainer: HTMLElement;

其中'd'是参考:

<div #mapContainer></div>
4

2 回答 2

4

像这样试试

@ViewChild('mapContainer', { static: true }) map: ElementRef;

ngOnInit() {
   console.log(this.map.nativeElement);
}

在这里您可以阅读更多关于 ViewChild 的信息。

于 2019-12-30T10:50:22.660 回答
1

ElementRefViewChild,一起使用

import { ElementRef, ViewChild } from '@angular/core';

@ViewChild('mapContainer', {static: false})
private el: ElementRef;

ngOnInit(){
    const element = this.el.nativeElement;
}
于 2019-12-30T10:49:23.730 回答