Initializing leaflet map in angular2 component after DOM object exists

后端 未结 2 1280
南方客
南方客 2021-01-14 05:36

I\'m currently trying to create a leaflet map inside an angular material2 tab-group as below

import {Component, NgModule} from \'@angular/core\         


        
相关标签:
2条回答
  • 2021-01-14 05:57

    Try reading the documentation for the lifecycle hook:

    https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html

    0 讨论(0)
  • 2021-01-14 06:10

    Looks like referencing directly the map container Element (through @ViewChild) is working without issue.

    import {Component, ViewChild} from '@angular/core';
    import 'leaflet';
    
    @Component({
        selector: 'minimap',
        template: `<div #mapDiv></div>`
    })
    export class MiniMap implements OnInit {
        @ViewChild('mapDiv') mapContainer;
    
        ngOnInit() {
            this.map = L.map(this.mapContainer.nativeElement);
        }
    }
    

    Updated Plunk: https://plnkr.co/edit/HGWb3J1f5HL8shFW9EUN?p=preview

    However, it seems that you need to re-initialize the map size once the tab is revealed (at least the first time). See Data-toggle tab does not download Leaflet map

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