简体   繁体   English

当我对src URL进行硬编码时,为什么我的img出现了,但是当我以编程方式分配它时却没有?

[英]Why does my img show up when I hardcode the src URL, but not when I assign it programmatically?

I'm creating a practice React app, and am using Firebase Storage to pull an image from my Firebase Storage bucket and show it on the website. 我正在创建一个练习React应用程序,并使用Firebase存储从我的Firebase存储桶中提取图像并在网站上显示。 The image, however, does not appear, despite the fact that the image URL is properly assigned to 's src. 但是,尽管图像URL已正确分配给src,但图像不会出现。 However, when I copy/paste the image URL directly into the src in JSX, everything shows up and works fine. 但是,当我将图像URL直接复制/粘贴到JSX中的src时,一切都会显示并正常工作。

My console.log() message shows the correct image URL is received before rendering. 我的console.log()消息显示在呈现之前接收到正确的图像URL。 Also, after the site has finished loading, running document.getElementsByClassName('Image').src in the developer tools console also shows the correct image URL. 此外,在网站加载完成后,在开发人员工具控制台中运行document.getElementsByClassName('Image')。src也会显示正确的图像URL。 When I hardcode the image URL into the src then everything works fine and the image displays. 当我将图像URL硬编码到src中时,一切正常,图像显示。

class App extends React.Component {

  constructor(props) {
    super(props); 
    this.state = {url: ''};  
    let storage = firebase.storage();
    let gsReference = storage.ref('blue.png');

    gsReference.getDownloadURL().then(url=> 
          {
              console.log("received URL: ", url); 
              this.setState({url: url})
          }
        );
  }


render() {

      document.getElementsByClassName('Image').src = this.state.url; 
      console.log("img URL: ", document.getElementsByClassName('Image').src);  

      return (
              <img src="" className='Image' /> 
      );
    }
}

Try this to see if it work for you 试试这个,看看它是否适合你

<img src={this.state.url} className="Image" />

Also set image src using document query is not a best practise 使用文档查询设置图像src也不是最佳实践

暂无
暂无

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

相关问题 尝试获取img src时为什么会得到浏览器URL? - Why am I getting the browser URL when I try to get the img src? 当我传递实际的 URL 时,Img src 标签正在工作,但是当我传递 URL 的常量 instesd 时,它不起作用 - Img src tag is working when I pass the actual URL but when I pass constant instesd of URL it does not work 当我关闭ColorBox窗口时,为什么在我的ColorBox“触发”周围会显示一个方形的边框? - Why does a squared border show up around my ColorBox “trigger”, when i close the ColorBox window? 当我使用Javascript innerHTML调用它时,为什么我的图像不显示? - Why does my image not show up when I use Javascript innerHTML to call it? 当我尝试为iframe src属性构建URL时,为什么AngularJS会抛出错误? - Why does AngularJS throw an error when I try to build a URL for an iframe src attribute? 滚动时,为什么我的网站在Chrome中显示滞后? - Why does my website show lag in Chrome when I scroll? 谷歌云存储图像 url 工作正常,但是当在 img html 文件的 src 中使用时,它会转到替代项并且不显示图像 - google cloud storage image url works fine but when used in the src of an img html file it goes to the alternative and does not show the image 悬停时如何让我的 img src 在 3-4 张图像之间不断变化? - How can I get my img src to keep changing between 3-4 images when hovered? 尝试在Colfusion中使用JavaScript将src属性添加到img标签时,为什么会出现错误? - Why am I getting an error when trying to add the src attribute to an img tag using JavaScript within Colfusion? 当我使用本地文件时,img src不起作用 - img src doesn't work when I use local files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM