javascript - 在 webpack 4 中导入图像
问题描述
嗨,我如何在 webpack 4 中导入图像?
这是我的 Webpack 配置。图像加载在静态文件夹中,但名称是神秘的。如果我想导入它并将其注销,则只有带有文件名的字符串。我怎样才能正确使用它?
这就是我使用它的方式
import img from "../../../../assets/hallo.png"
<img style = "height: 100px;width: 100px" src="${img}" />
Webpack 配置
const path = require('path')
const BrowserSyncPlugin = require("browser-sync-webpack-plugin")
var webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports ={
entry: './src/js/index.js',
output: {
path: path.resolve(__dirname, 'static'),
filename: 'monitor-bundle.js'
},
devtool: 'source-map',
mode: 'development',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract(
{
fallback: 'style-loader',
use: ['css-loader']
})
},
{
test: /\.(jpg|png)$/,
use: {
loader: "file-loader",
}
},
]
},
watch: true,
plugins: [
new BrowserSyncPlugin({
watchOptions: {
poll: true
},
host: "localhost",
port: "1337",
proxy: "http://localhost:80/",
files: ["./static/monitor-bundle.js"],
open: true,
reloadDelay: 0,
reloadDebounce: 0,
browser: ["chromium", "google chrome"]
}),
new ExtractTextPlugin({filename: 'monitor-style.css'})
],
};
解决方案
您只是在这里传递一个空字符串作为源。
<img style = "height: 100px;width: 100px" src="${img}" />
如果你想使用这个 ${},你需要使用模板字符串
<img style = "height: 100px;width: 100px" src=`${img}` />
在这种情况下,您可以这样做
<img style = "height: 100px;width: 100px" src={img} />
推荐阅读
- python-3.x - 将参数 url 更改为本地 ( raise ValueError("unknown url type: %r" % self.full_url))
- javascript - How to extract property values in string form, to loop over them
- java - InfluxDB 异常:org.influxdb.InfluxDBException: {"code":"unauthorized","message":"Unauthorized"}
- javascript - 计算对象数组中值的总和
- python - pandas.io.sql.DatabaseError:sql'utf-8'编解码器执行失败无法解码位置0的字节0xa0:无效的起始字节
- php - 如何进行 cron 作业以从 PHP Smarty Index.tpl 保存到 Index.html?
- python - 有什么方法可以使用 Xlib Python 将 google colab display 连接到我们的系统?
- python-3.x - 结合不同的seaborn分布图
- azure - 拒绝:在 Azure DevOps 管道中检索权限失败
- python - 即使条件为假,While 循环仍在运行