Skip to content
This repository has been archived by the owner on Jan 27, 2023. It is now read-only.

How to load nuxt-leaflet locally? #77

Open
d4rkr3pt0r opened this issue Apr 11, 2021 · 5 comments
Open

How to load nuxt-leaflet locally? #77

d4rkr3pt0r opened this issue Apr 11, 2021 · 5 comments

Comments

@d4rkr3pt0r
Copy link

By adding 'nuxt-leaflet', in nuxt.config.js, it is loading on all pages. But I need to import leaflet on ONE page only to reduce load size. How Can I do this?

@d4rkr3pt0r
Copy link
Author

Here is the solution:

let LMap, LTileLayer, LMarker, LPopup, LIcon;
if (process.client) {
	require("leaflet");
	({
		LMap,
		LTileLayer,
		LMarker,
		LPopup,
		LIcon
	} = require("vue2-leaflet/dist/vue2-leaflet.min"));
}
import 'leaflet/dist/leaflet.css'
export default {
	components: {
		LMap, LTileLayer, LMarker, LPopup, LIcon
	},
	// The rest of the code

@ayalon
Copy link

ayalon commented Apr 28, 2021

Thanks! Had the same issue and save 421kb in the vendor bundle thanks to this solution.

@Tobeyforce
Copy link

Tobeyforce commented Jul 17, 2021

Here is the solution:

let LMap, LTileLayer, LMarker, LPopup, LIcon;
if (process.client) {
	require("leaflet");
	({
		LMap,
		LTileLayer,
		LMarker,
		LPopup,
		LIcon
	} = require("vue2-leaflet/dist/vue2-leaflet.min"));
}
import 'leaflet/dist/leaflet.css'
export default {
	components: {
		LMap, LTileLayer, LMarker, LPopup, LIcon
	},
	// The rest of the code

I got everything to work, awesome! Just one more thing.
My console is throwing this error:
http://localhost:3000/_nuxt/node_modules/leaflet/dist/images/marker-shadow.png 404 (Not Found)
It also can't find the marker-icon.png either in prod it seems.
I don't understand why, I don't use these, I got my own custom icons..

Any chance this is fixable?

My code:

let LMap, LTileLayer, LMarker, LPopup, LIcon, LControlAttribution, LControlZoom
if (process.client) {
  require("leaflet");
  ({
    LMap,
    LTileLayer,
    LMarker,
    LPopup,
    LIcon,
    LControlAttribution,
    LControlZoom

  } = require("vue2-leaflet/dist/vue2-leaflet.min"));
}
import "leaflet/dist/leaflet.css";
export default {
    components: {
    "l-map": LMap,
    "l-tile-layer": LTileLayer,
    "l-marker": LMarker,
    "l-popup": LPopup,
    "l-icon": LIcon,
    "l-control-attribution": LControlAttribution,
    "l-control-zoom": LControlZoom
  },
....
  methods: {
    setIconStyles() {
      
      this.icon = this.$L.icon({
        shadowUrl: "/icon_shadow_7.png",
        iconUrl: "/housemarkerblue1.png",
        shadowAnchor: [10, 45],
        iconAnchor: [16, 37],
        popupAnchor: [-5, -35],
        // iconUrl: "/marker_black_2.png",
        iconSize: [23, 33],
        // staticAnchor: [30,30],
      });
  
    },
  },
....

Edit: It seems like vue2-leaflet-markercluster.js is causing the problem.. any ideas?

@luchito93
Copy link

luchito93 commented Feb 4, 2022

you can do something like

/**
 * Use leaflet open maps
 */
let LMap, LMarker, LTileLayer
if (process.client) {
  require('leaflet')
  ;({
    LMap,
    LMarker,
    LTileLayer
  } = require('vue2-leaflet/dist/vue2-leaflet.min'))

  // fix error icon - webpack leaflet
  let { Icon } = require('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')
  })
}
import 'leaflet/dist/leaflet.css'
export default { // the rest of you code

taked from here https://vue2-leaflet.netlify.app/quickstart/#nuxt

@SoftCreatR
Copy link

SoftCreatR commented Feb 15, 2022

What works for me is:

  delete L.Icon.Default.prototype._getIconUrl
  L.Icon.Default.mergeOptions({
    shadowUrl: ''
  })

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants