Vue2leaflet: Cannot read property 'mapObject' in Nuxt project.

Created on 13 Aug 2019  路  8Comments  路  Source: vue-leaflet/Vue2Leaflet

I have done the leaflet import to the Nuxt project like the documentations says.
Everything for fine but when I try to access the API by calling

mounted () {
      this.$nextTick(() => {
        this.map = this.$refs.myMap.mapObject;
        console.log(this.map)
      })
    },`

I get vendor error
TypeError: Cannot read property 'mapObject' of undefined
at VueComponent. (index.js:236)
at Array. (commons.app.js:11439)
at flushCallbacks (commons.app.js:11365)

Here is the the leaflet.js in the plugin folder

import Vue from 'vue';
import { LMap, LTileLayer, LMarker, findRealParent, propsBinder, } from 'vue2-leaflet';
import * as L from "leaflet";
import Vue2LeafletLocatecontrol from 'vue2-leaflet-locatecontrol';

import { Icon } from 'leaflet';
delete Icon.Default.prototype._getIconUrl;

Icon.Default.mergeOptions({
  iconRetinaUrl: require('leaflet/dist/images/marker-icon-2x.png'),
  iconUrl: require('leaflet/dist/images/marker-icon.png'),
  shadowUrl: require('leaflet/dist/images/marker-shadow.png')
});

Vue.component('l-map', LMap);
Vue.component('l-tile-layer', LTileLayer);
Vue.component('l-marker', LMarker);
Vue.component('v-locatecontrol', Vue2LeafletLocatecontrol);

delete Icon.Default.prototype._getIconUrl;

Icon.Default.mergeOptions({
  iconRetinaUrl: require('leaflet/dist/images/marker-icon-2x.png'),
  iconUrl: require('leaflet/dist/images/marker-icon.png'),
  shadowUrl: require('leaflet/dist/images/marker-shadow.png')
});


const LeafletPlugin = {
  install(Vue) {
    // Expose Leaflet
    Vue.prototype.$L = L;
  }
};

What I do wrong?

Versions

  • Vue2Leaflet: 1.5.1+build.2e3e0ffb

Most helpful comment

Try to access the mapObject not in mounted but with the l-map ready event:
https://korigan.github.io/Vue2Leaflet/#/components/l-map/?id=events
That is the safest way :)

Most probably you are using the mounted hook of the component that contains the no-ssr with inside the l-map right?

All 8 comments

Try to access the mapObject not in mounted but with the l-map ready event:
https://korigan.github.io/Vue2Leaflet/#/components/l-map/?id=events
That is the safest way :)

Most probably you are using the mounted hook of the component that contains the no-ssr with inside the l-map right?

Thanks @DonNicoJs
I got it working also inside methods

 methods: { /* Any app-specific functions go here */ 
        centerUpdated (center) {
            this.center = center;
        },
        boundsUpdated (bounds) {
            this.bounds = bounds;
            console.log(this.$refs.myMap.mapObject.getBounds())
        }
    }

Now it returns the object.

@vjandrei Can we close the issue so ?

@DonNicoJs Yes :) Thanks.

Try to access the mapObject not in mounted but with the l-map ready event:
https://korigan.github.io/Vue2Leaflet/#/components/l-map/?id=events
That is the safest way :)

Most probably you are using the mounted hook of the component that contains the no-ssr with inside the l-map right?

Why not put this in docs example.. spend some time until found this issue

@bravik PR welcomed :) FAQ section should be the appropriate place

@bravik merged!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mawieutc picture mawieutc  路  4Comments

udos picture udos  路  4Comments

mpallante picture mpallante  路  4Comments

zaqisilverano picture zaqisilverano  路  4Comments

lusman20 picture lusman20  路  4Comments