使用vue cli。Vue Cli Webpack後臺url路徑問題
由於某些原因,如果我直接在scss中引用它們,並且不將它們動態綁定到我的vue對象,則會出現相關路徑問題。
說我有一個名爲框的div的vue模板。 箱有這樣的背景下網址:
.box的{ 背景:網址( '../ IMG/box.jpg')
當我運行NPM運行dev的那本地工作就好了。 但是當我運行構建,它不起作用。 404錯誤。 我也試着這樣做:
.box{
background: url('~../img/box.jpg')
並沒有奏效。
因此,有這樣的:
webpack css-loader not finding images within url() reference in an external stylesheet
當我改變這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>
問題是,我是否必須綁定數據中的每一個單獨的img ...或者我不能直接引用圖像,如果我知道它不會被改變。
或者我的sass加載器/ 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()
])
}