最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to dynamically display an image in VueJS? - Stack Overflow

programmeradmin1浏览0评论

Please see here the relations between ponents How to pass value from one child ponent 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 es from another ponent 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.

Please see here the relations between ponents How to pass value from one child ponent 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 es from another ponent 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.

Share Improve this question edited Jan 22, 2020 at 13:33 Askar asked Jan 22, 2020 at 13:29 AskarAskar 5,85411 gold badges55 silver badges99 bronze badges 5
  • what does your file structure look like? – depperm Commented Jan 22, 2020 at 13:31
  • Thanks for your time. I have updated my post. The code structure can be seen here stackoverflow./q/59855921/1745902 – Askar Commented Jan 22, 2020 at 13:33
  • does this answer your question: stackoverflow./a/40493036/3462319 – depperm 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 ponent. Maybe I could use that answer as hint. I will try. I'm new to VueJS. – Askar 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... – Askar Commented Jan 22, 2020 at 13:48
Add a ment  | 

3 Answers 3

Reset to default 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/guide/html-and-static-assets.html#static-assets-handling

Don't forget to add.

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

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

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

};
发布评论

评论列表(0)

  1. 暂无评论