简体   繁体   English

如何从 API 调用后台动态图像?

[英]How to call Dynamic Image in Background from API?

I am trying to call an Image dynamically from thesportsdb API.我正在尝试从sportsdb API 动态调用图像。 查看我的代码

After destructuring, I am trying to set the image in the background of this div.解构后,我试图在这个 div 的背景中设置图像。 But It doesn't show the image.但它没有显示图像。 It only works when I add a static image in CSS.它仅在我在 CSS 中添加 static 图像时才有效。 It's a react practice project and everything else is working fine.这是一个反应练习项目,其他一切都很好。

How may I solve this?我该如何解决这个问题?

<div style={{background: strStadiumThumb}} className="banner-img">
    <img className='img-fluid' src={strTeamBadge} alt="" />
 </div>

Firstly, please add the relevant code directly into the question, not pictures of code.首先,请直接将相关代码添加到问题中,而不是代码图片。 Secondly, background isn't a valid div tag attribute.其次, background不是有效的div标签属性。 If you want to inline the background style, you can use the style attribute, which is an object property that lets you inline CSS styles into your element:如果要内联背景样式,可以使用style属性,这是一个 object 属性,可让您将 CSS styles 内联到元素中:

<div style={{ backgroundImage: `url(${strStadiumThumb})` }} className="banner-img>
    ...

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

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