Vue components css background-image path error

Wang.Shayne picture Wang.Shayne · Mar 11, 2017 · Viewed 14.9k times · Source

output

    output: {
        path: config.build.assetsRoot,
        publicPath: process.env.NODE_ENV === 'production' ? 
        config.build.assetsPublicPath : config.dev.assetsPublicPath,
        filename: '[name].js'
    }

base

  build: {
    env: require('./prod.env'),
    index: path.resolve(__dirname, '../dist/index.html'),
    assetsRoot: path.resolve(__dirname, '../dist'),
    assetsSubDirectory: 'static',
    assetsPublicPath: './',
    productionSourceMap: false,
    productionGzip: false,
    productionGzipExtensions: ['js', 'css']
  },

after build,the index pages work normal, css background-image path like this

background: url(./static/img/bg_certificate.c5cad1e.png) no-repeat 50%;

but the component css background-image path error, like this

background: url(static/img/btn_my.b9186cc.png) no-repeat 50%;

It's look like the path lose "./",

Answer

Saurabh picture Saurabh · Mar 11, 2017

Instead of absolute path, You should give the relative path from the current file location with webpack in .vue file, something like:

background: url(../../static/img/btn_my.b9186cc.png) no-repeat 50%;