1

Please see here the relations between components How to pass value from one child component to another in VueJS?

The snippet bellow not giving an error and not displaying the image

<img v-bind:src="image_url" /> 

Code:

<template>
  <div>
    <img v-bind:src="image_url" />
  </div>
</template>

<script>
export default {
  props: ["image_url"]
};
</script>

image_url comes from another component and in VueJS developer's tool I was able to confirm the value is an url. Looks like something wrong with the way how I'm trying to render a dynamic image.

enter image description here

5
  • what does your file structure look like? Commented Jan 22, 2020 at 13:31
  • Thanks for your time. I have updated my post. The code structure can be seen here stackoverflow.com/q/59855921/1745902 Commented Jan 22, 2020 at 13:33
  • does this answer your question: stackoverflow.com/a/40493036/3462319 Commented Jan 22, 2020 at 13:40
  • It looks like it's showing how to render for multiple images, where in my case I'm dealing with one URL in the ItemImage component. Maybe I could use that answer as hint. I will try. I'm new to VueJS. Commented Jan 22, 2020 at 13:45
  • also that post is from 2016. I believe this can be done in a better way these days... Commented Jan 22, 2020 at 13:48

3 Answers 3

6

You have to expose your images to your web server. Hopefully Vue-cli does that for you. You have to move your assets folder from src to the public folder.

More info on https://cli.vuejs.org/guide/html-and-static-assets.html#static-assets-handling

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

Comments

0

Don't forget to add.

<div v-if="image_url">
  <img v-bind:src="image_url" />
</div>

5 Comments

As a side note image urls typically begin with a forward slash. /assets/images/apple.jpg
Thanks. Adding the snippet and slashes in the url didn't help
Double check the image exists in your assets/images folder.
Images are there. When I tried explicitly specify one url, it worked. So something wrong with how I'm trying to render
Inspect the element in dev tools. Is there a path to the image in the src.
0

Might worked on yours. Make a watcher for that to update again the props value an rerender it in your component.

export default {
  props: ["image_url"],
  watch: {
          image_url(val){
              this.image_url = val
          },
    }

};

1 Comment

Hi. Can you please add more explanation how to add it and where?

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.