[英]Can I use require('img.jpg') to load img file on local server within background-image style property?
Can I use require('img.jpg') to load img file on local server within background-image style property? 我可以使用require('img.jpg')在background-image样式属性内在本地服务器上加载img文件吗?
To load img files in a react app on a local server I use <img src= {require('./greenLine.jpg')}
/> after installing url-loader
and file-loader
. 要在本地服务器上的<img src= {require('./greenLine.jpg')}
应用程序中加载img文件,请在安装url-loader
和file-loader
之后使用<img src= {require('./greenLine.jpg')}
/> />。
This works great, but I want to use the img file in a style object, like - const divBkground = { backgroundImage: require('url(./greenLine.jpg)')
} 这很好用,但是我想在样式对象中使用img文件,例如const divBkground = { backgroundImage: require('url(./greenLine.jpg)')
}
This doesn't work. 这行不通。 Can someone advise? 有人可以建议吗?
THANKS.. 谢谢..
You need not use require
when you try to define a style for background image . 尝试为背景图像定义样式时,无需使用require
。 Just the simple backgroundImage: 'url(./greenLine.jpg)'
works fine 只是简单的backgroundImage: 'url(./greenLine.jpg)'
可以正常工作
const divBkground = {
backgroundImage: 'url(./greenLine.jpg)'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.