Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue components css background-image path error

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 "./",

like image 806
Wang.Shayne Avatar asked Mar 11 '17 16:03

Wang.Shayne


4 Answers

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%;
like image 165
Saurabh Avatar answered Nov 15 '22 16:11

Saurabh


I also come by this issue when I tried using styles in component of Vue 3. I solved it by using the following syntax.

background-image: url('~@/assets/image.png');
like image 29
Usama Imdad Avatar answered Nov 15 '22 15:11

Usama Imdad


What about setting:

build: {
  ...
  assetsSubDirectory: '',
  assetsPublicPath: '/static/',
  ...
}

and so:

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

Working with relative paths is, in general lines, a nightmare.

like image 5
Evhz Avatar answered Nov 15 '22 15:11

Evhz


:style="{ backgroundImage: 'url(\'' + require('@/assets/bg3.jpg') + '\')' }

This will be the Best solution for the background image.

like image 4
Awais Jameel Avatar answered Nov 15 '22 14:11

Awais Jameel