Angular 6 - Rendering problems if BrowserAnimationsModule imported (Openlayers)

前端 未结 2 532
醉梦人生
醉梦人生 2020-12-21 22:23

I basically have a App which renders a small map. On this page is also a \"toggle\" button which hides the small map with a *ngIf in the parent component and displays the ma

相关标签:
2条回答
  • 2020-12-21 22:34

    You have to use ngAfterViewInit, then you can be 'sure' the element inside your components template is present in the DOM.

    ngAfterViewInit() {
      this.map = new OlMap({
        target: 'map',
        layers: [new OlTileLayer({ source: new OlOSM() })],
        view: new OlView({ zoom: 13, center: fromLonLat([13.376935, 52.516181]) }),
        controls: []
      });
    }
    
    0 讨论(0)
  • 2020-12-21 22:46

    Here is a solution,

    Component template

    <div #mapElementRef class="map"></div>
    

    Component class implements OnInit and has the next property

    @ViewChild('mapElementRef') mapElementRef: ElementRef;
    

    finally initialize the map on ngOnInit method, you can use

    this.map.setTarget(this.mapElementRef.nativeElement);
    

    Hope it helps.

    0 讨论(0)
提交回复
热议问题