Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue, Leaflet, accessing ref object / function

I have a running leaflet map in a vue project. Eg (only relevant vue code included).

<div style="height:70vh; width:100%; position: relative;">
      <l-map ref="map" :zoom="maps_obj.zoom" :center="[maps_obj.latitude, maps_obj.longitude]">
        <l-tile-layer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" layer-type="base"></l-tile-layer>
      </l-map>
</div>

<script>
import 'leaflet/dist/leaflet.css';
import { defineComponent, ref } from 'vue';
import {
  LMap, LMarker, LTileLayer, LIcon,
} from '@vue-leaflet/vue-leaflet';
import { icon } from 'leaflet';

methods: {
    test() {
      this.$nextTick(() => {
        this.$refs.map.mapObject.invalidateSize();
      });
    },
  },
</script>

The map itself works perfectly. But how come I cannot access the leaflet api and run a function, like the documentation states? (https://vue2-leaflet.netlify.app/quickstart/#accessing-leaflet-api)

It just returns the error:

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'invalidateSize')
 
like image 362
AP87 Avatar asked Oct 12 '25 12:10

AP87


1 Answers

Of course after posting this question I found the "hidden answer".. The mapObject is nowadays called leafletObject..

this.$refs.map.leafletObject.invalidateSize();
like image 171
AP87 Avatar answered Oct 14 '25 12:10

AP87