简体   繁体   English

如何在VueJS中动态显示图像?

[英]How to dynamically display an image in VueJS?

Please see here the relations between components How to pass value from one child component to another in VueJS?请看这里组件之间的关系如何在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. image_url来自另一个组件,在 VueJS 开发人员的工具中,我能够确认该值是一个 url。 Looks like something wrong with the way how I'm trying to render a dynamic image.看起来我尝试渲染动态图像的方式有问题。

在此处输入图片说明

You have to expose your images to your web server.您必须将图像公开到您的网络服务器。 Hopefully Vue-cli does that for you.希望 Vue-cli 为您做到这一点。 You have to move your assets folder from src to the public folder.您必须将资产文件夹从 src 移动到公共文件夹。

More info on https://cli.vuejs.org/guide/html-and-static-assets.html#static-assets-handling有关https://cli.vuejs.org/guide/html-and-static-assets.html#static-assets-handling 的更多信息

Might worked on yours.可能对你有用。 Make a watcher for that to update again the props value an rerender it in your component.为它创建一个观察者以再次更新 props 值并在您的组件中重新渲染它。

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

};

Don't forget to add.不要忘记添加。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM