[英]images not loading in a react project using webpack 2
I'm trying to render images like this but they're showing up as broken images on the pages. 我正在尝试渲染这样的图像,但它们在页面上显示为损坏的图像。
...
return (
<img src='/public/images/ic-account-circle-black-24-px-5.png' alt='uimg' />
)
Here is my webpack config for handling image files: 这是我用于处理图像文件的webpack配置:
config.module.rules.push({
test : /\.(png|jpg|gif)$/,
loader : 'url-loader',
options : {
limit : 8192,
},
})
I think webpack by default doesn't detect image src in JSX. 我认为默认情况下,webpack不会在JSX中检测到图像src。 You need to be explicit about it with a require
您需要对它明确要求
https://github.com/petehunt/webpack-howto/issues/11 https://github.com/petehunt/webpack-howto/issues/11
You need to require the image for webpack to resolve it using the url-loader
您需要提供用于webpack的图像才能使用
url-loader
解析它
Example
var AccountCircle = require('src/images/ic-account-circle-black-24-px-5.png');
class Component extends React.Component {
render() {
return (
<img src={AccountCircle} />
)
}
}
Webpack will resolve the require
using the url-loader
, move it to your destination folder and automatically insert the url. Webpack将使用
url-loader
解决require
,将其移动到目标文件夹并自动插入url。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.