2

I'm having trouble trying to create a icon in Leaflet using Vuejs. It doesn't show the icon and I don't know how to fix the path. I have tried all the paths I can think of:

  • assets/leaf-green.png
  • /assets/leaf-green.png
  • ../assets/leaf-green.png
  • ../../assets/leaf-green.png
  • src/assets/leaf-green.png
  • ../src/assets/leaf-green.png
  • ../../src/assets/leaf-green.png

Code (Map.vue):

const greenIcon = L.icon({
  iconUrl: "assets/leaf-green.png",
  iconSize: [38, 95],
  iconAnchor: [22, 94],
  popupAnchor: [-3, -76]
});

Project structure:

enter image description here

DevTools Network tab shows that it tries to load this url: http://localhost:8080/assets/leaf-green.png

Any ideas?

2
  • Did you check the Network tab in chrome development tools to see where it tries to load it from? Commented Mar 2, 2018 at 11:42
  • Network tries to load: localhost:8080/assets/leaf-green.png and /assets/... didn't work Commented Mar 2, 2018 at 11:44

2 Answers 2

1

/assets is not served. The images become src="data:image/png;base64,iVBORw0K...YII=" strings.

Solution: use relative path.

For example, say you have the following folder structure:

- src
  +- assets
     - myImage.png
  +- components
     - MyComponent.vue

If you want to reference the image in MyComponent.vue, the path sould be ../assets.myImage.png

Check this demo codesandbox: https://codesandbox.io/s/610zwq4o6w?module=%2Fsrc%2Fcomponents%2FHelloWorld.vue

Using from JavaScript

Use require('../assets/myImage.png')

Sign up to request clarification or add additional context in comments.

4 Comments

can you tell me how? I've tried ../assets/leaf-green.png and ../../assets/leaf-green.png but doens't work either
I can load images with relative path in the template part, but I cannot use that same url in the JS part :S
Use require()
This just did not work for me. With my learning curve the one word hint of 32-goldmedal acdcjunior 'Use require()' was not applicable. Ok, I did not give up and created another answer which I hope helps faster now for people of my knowledge level of vue.
0

Ok, these up to date already exisiting did not yield me straight to a working solution. I checked this https://stackoverflow.com/a/50866199/845117 to get the correct hint which is:

 const greenIcon = L.icon({
     iconUrl: require('./assets/leaf-green.png'),
     iconSize: [38, 95],
     iconAnchor: [22, 94],
     popupAnchor: [-3, -76]
 });

Whether it is ./assets, ../assets or even something like ../../assets depends on your structure where you place your .vue-components.
It is relative to the path from where you want to call/reference it.
As I understood you always have to start with at least one dot.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.