Webpack 网页包未从CSS背景图像属性加载图像
我不知道该怎么办。。。我正在使用带有CSS、样式、sass和手写笔加载程序的Webpack。这是我的网页配置。。。当我使用以下内容时,图像不会显示:Webpack 网页包未从CSS背景图像属性加载图像,webpack,vue.js,Webpack,Vue.js,我不知道该怎么办。。。我正在使用带有CSS、样式、sass和手写笔加载程序的Webpack。这是我的网页配置。。。当我使用以下内容时,图像不会显示: body background-image: url('bg.jpg') 如果我在HTML中包含带有标记的图像,它们会正常工作 我的网页包配置: 您需要使用~。这告诉Sass加载程序使用webpack的require解析来解析文件路径;默认情况下,它将像普通Sass图像一样解析url()。因此,它应该是背景图像:url('~bg.jpg'
body
background-image: url('bg.jpg')
如果我在HTML中包含带有
标记的图像,它们会正常工作
我的网页包配置:
您需要使用
~
。这告诉Sass加载程序使用webpack的require
解析来解析文件路径;默认情况下,它将像普通Sass图像一样解析url()
。因此,它应该是背景图像:url('~bg.jpg')
——然后确保webpack知道(通过您的配置)如何找到bg.jpg所在的目录。我会尝试使用url(require('myimage.png'),尽管我不是舒尔,这是我一直在寻找的最佳答案。非常感谢。
var path = require('path')
module.exports = {
entry: {
app: './src/main.js'
},
output: {
path: path.resolve(__dirname, '../dist/static'),
publicPath: '/static/',
filename: '[name].js'
},
resolve: {
extensions: ['', '.js', '.vue'],
alias: {
'src': path.resolve(__dirname, '../src')
}
},
resolveLoader: {
root: path.join(__dirname, 'node_modules')
},
module: {
preLoaders: [
{
test: /\.vue$/,
loader: 'eslint',
exclude: /node_modules/
},
{
test: /\.js$/,
loader: 'eslint',
exclude: /node_modules/
}
],
loaders: [
{
test: /\.vue$/,
loader: 'vue'
},
{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/
},
{
test: /\.json$/,
loader: 'json'
},
{
test: /\.scss$/,
loaders: ['style', 'css?sourceMap', 'sass?sourceMap']
},
{
test: /\.woff/,
loader: 'url?prefix=font/&limit=10000&mimetype=application/font-woff'
}, {
test: /\.ttf/,
loader: 'file?prefix=font/'
},
{
test: /\.eot/,
loader: 'file?prefix=font/'
},
{
test: /\.svg/,
loader: 'file?prefix=font/'
},
{
test: /\.(jpe?g|png|gif|svg)$/i,
loaders: [
'url?limit=10000&name=[name].[ext]?[hash:7]',
'image-webpack?{progressive:true, optimizationLevel: 7, interlaced: false, pngquant:{quality: "65-90", speed: 4}}'
]
}
]
},
eslint: {
formatter: require('eslint-friendly-formatter')
},
vue: {
loaders: {
sass: 'style!css!sass?indentedSyntax'
}
}
}