If you log inside componentDidMount
the reference to the Map
componentDidMount() {
const map = this.mapRef.leafletElement;
console.log(map)
}
and expand in the console on the bottom __proto__: NewClass
under _zoomBoundLayers: {26: NewClass}
you can see the methods that are inherited and that invalidateSize
is displayed and therefore provided there.
Edit
I thought you were using react-leaflet.
Without the use of react leaflet you can use the following code to get a reference to the map instance.
class Map extends Component {
componentDidMount() {
const map = this.map = L.map('map').setView([51.505, -0.09], 13);
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
maxZoom: 18,
attribution: 'Map data © <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, ' +
'<a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
'Imagery ? <a href="https://www.mapbox.com/">Mapbox</a>',
id: 'mapbox.streets'
}).addTo(map);
console.log(this.map)
}
render() {
return (
<div id="map"/>
);
}
}
and then do as in the react-leaflet version: expand in the console on the bottom __proto__: NewClass
under _zoomBoundLayers: {26: NewClass}
you can see the methods that are inherited and that invalidateSize
is displayed and therefore provided there.
Demo
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…