使用Vue CLI。
由于某些原因,如果我在scss中直接引用图像并且没有将它们动态绑定到我的Vue对象上,其中一些图片的相对路径会出现问题。
假设我有一个名为“box”的div的Vue模板。 Box的背景url如下:
.box{ background: url('../img/box.jpg')
当我运行npm run dev时,它在本地工作得很好。 但是当我运行构建时,它就无法工作了,显示404错误。 我还尝试过这样做:
.box{
background: url('~../img/box.jpg')
但那没起作用。
所以有这个:
webpack css-loader在外部样式表中的url()引用中找不到图像
当我在webpack.config中更改这个时:
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
收件人:
output: {
path: path.resolve(__dirname, './dist'),
publicPath: './dist/',
filename: 'build.js'
},
它将在我的webpack构建中创建带有哈希值的块图像以进行缓存。仅适用于那些未绑定在vue对象中的特定图像。
然后,我必须将这些图像拖到根目录下的dist文件夹中......而不是我想要的将它们保留在相对于HTML文件的img文件夹中(HTML文件非常简单):
<html lang="en">
<head>
<meta charset="utf-8">
<title>vue</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<app></app>
<script src="dist/build.js"></script>
</body>
</html>
问题是,我是否需要绑定每一个来自数据的Vue图像...还是只有当我知道它不会被更改时才可以直接引用图像。
或者是我在sass loader/webpack中漏掉了一些设置。
这是我的webpack配置:
var path = require('path')
var webpack = require('webpack')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: './dist/',
filename: 'build.js'
},
resolveLoader: {
root: path.join(__dirname, 'node_modules'),
},
vue: {
html: {
root: path.resolve(__dirname, './dist')
}
},
module: {
loaders: [
{
test: /\.vue$/,
loader: 'vue'
},
{
test: /\.js$/,
loader: 'babel',
exclude: /node_modules/
},
{
test: /\.json$/,
loader: 'json'
},
{
test: /\.html$/,
loader: 'vue-html'
},
{
test: /\.scss$/,
loaders: ["style", "css", "sass"]
},
{
test: /\.(png|jpg|gif|svg)$/,
loader: 'url',
query: {
limit: 10000,
name: '[name].[ext]?[hash]'
}
}
]
},
devServer: {
historyApiFallback: true,
noInfo: true
},
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}),
new webpack.optimize.OccurenceOrderPlugin()
])
}