[英]Loading images with reactjs and webpack
I am having issues trying to render images onto the webpage after I bundle everything with webpack. 将所有内容与webpack捆绑在一起后,尝试将图像呈现到网页上时遇到问题。 Below is my webpack.config.js file 下面是我的webpack.config.js文件
var path = require('path');
module.exports = {
entry: './src/main/dashboardapp/src/index.js',
devtool: 'sourcemaps',
cache: true,
output: {
path: __dirname,
filename: './src/main/resources/static/built/bundle.js'
},
module: {
loaders: [
{ test: /\.woff2/, exclude: /(node_modules)/, loader: 'ignore-loader' },
{ test: /\.wof/, exclude: /(node_modules)/, loader: 'ignore-loader'},
{ test: /\.ttf/, exclude: /(node_modules)/, loader: 'ignore-loader'},
{ test: /\.svg/, exclude: /(node_modules)/, loader: 'ignore-loader'},
{ test: /\.eot/, exclude: /(node_modules)/, loader: 'ignore-loader'},
{ test: /\.png/, exclude: /(node_modules)/, loader: 'ignore-loader'},
{ test: /\.bmp/, exclude: /(node_modules)/, loader: 'ignore-loader'},
{ test: /\.ico/, exclude: /(node_modules)/, loader: 'ignore-loader'},
{ test: /\.png/, loader: 'file-loader' },
{ test: /\.bmp/, loader: 'file-loader' },
{ test: /\.ico/, loader: 'file-loader' },
{ test: /\.css$/,
exclude: /(node_modules)/,
loaders: ['style-loader', 'css-loader']
},
{
test: /\.css$/,
include:
[path.resolve(__dirname, "src/main/dashboardapp/node_modules/react-table/")],
loaders: ['style-loader', 'css-loader']
},
},
{
test: /\.js$/,
exclude: /(node_modules)/,
loader: 'babel-loader',
query: {
cacheDirectory: true,
presets: ['es2015', 'react'],
plugins: ['transform-class-properties']
}
},
{
test: /\.(jpg|png|gif|svg|pdf|ico)$/,
use: [
{
loader: 'file-loader',
options: {
name: '[path][name]-[hash:8].[ext]'
},
},
]
},
]
}
};
here is where I am rendering the image inside of reactjs 这是我在reactjs内渲染图像的地方
<img alt="" src={require('.././images/GreenCheck.bmp')} />
Inside the inspector it looks like this 在检查器内部看起来像这样
[object Object]
Right now the images folder is sitting one directory up and then going into images. 现在,images文件夹位于一个目录中,然后进入images。 Any help I would be greatly appreciated it. 任何帮助,我将不胜感激。
You may want to consider importing your image separately. 您可能需要考虑单独导入图像。 I would re-explain it but I think its easier to look here: how to import a picture in the same directory . 我会重新解释它,但我认为在这里更容易理解: 如何在同一目录中导入图片 。
Hope this helps! 希望这可以帮助!
Try importing the image as a variable and use the variable in your render method. 尝试将图像作为变量导入,然后在渲染方法中使用该变量。
import GreenCheck from '.././images/GreenCheck.bmp';
...
<img src={GreenCheck} />
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.