简体   繁体   English

使用反应样式设置背景图像

[英]setting background-image using react styling

i am making a project of my own and i am trying to display 3 components that will render a different image for one of them.我正在制作自己的项目,我正在尝试显示 3 个组件,这些组件将为其中一个呈现不同的图像。 The image is not displaying, i think the problem is the server is downloading the image as a html/text type.图像未显示,我认为问题是服务器正在将图像下载为 html/文本类型。 I am new to react and i am having difficulties displaying images...我是新来的反应,我在显示图像时遇到困难......

style={{backgroundImage: url(${props.img}) } } style={{backgroundImage: url(${props.img}) } }

img={city.imageURL} img={city.imageURL}

imageURL: '/assets/mount-fugji.png' imageURL: '/assets/mount-fugji.png'

And by the way, how do i access the style object?顺便说一句,我如何访问样式 object?

In React you should use:在 React 中你应该使用:

styles={{ backgroundImage:`url(${picture})` }}

so you can try to edit code like this:所以你可以尝试像这样编辑代码:

style={{backgroundImage: `url(${props.img})`} }

you can use like你可以像这样使用

 <div style={{ backgroundImage: `url(${props.img || city.imageURL})` }} />;

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

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