【发布时间】:2018-12-26 01:47:23
【问题描述】:
我无法在 React 组件中显示图像。经过多次试验(尝试this、this、this、this、this、this、this 和this)并且只有错误,我请求帮助。我正在开发构建(不是生产构建)。
我仍然收到此错误:
Module parse failed: /project/src/images/net.png Unexpected character '�' (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
组件
import thumbnail from '../images/net.png';
<img src={thumbnail}/>
Webpack 配置:
devtool: 'cheap-module-eval-source-map',
entry: [
'eventsource-polyfill',
'webpack-hot-middleware/client',
'./src/index'
],
target: 'web',
output: {
path: path.resolve(__dirname, 'dist'),
task `npm run build`.
publicPath: 'http://localhost:3000/',
filename: 'bundle.js'
},
devServer: {
contentBase: './src'
},
plugins: [new webpack.HotModuleReplacementPlugin(), new webpack.NoEmitOnErrorsPlugin()],
module: {
rules: [
{
test: /\.js$/,
include: path.join(__dirname, 'src'),
loader: 'babel-loader'
},
{
test: /(\.css)$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: { sourcemap: true }
}
]
},
{
test: /\.(svg|png|jpg|jpeg|gif)$/,
include: './src/images',
use: {
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
outputPath: paths.build
}
}
}
]
}
目录结构
Project
-- src
-----components
-----images
-----index.js
如何显示图像?
此处的示例代码:githublink 见 /src/components/home/HomePage.js
我该怎么做才能看到主页上的图像?
【问题讨论】: