vue css引用背景图片问题

2019-08-25  本文已影响0人  minusplus

在css中的 background 属性中,正常是使用 background: url('../assets/img/hello.png'); 就能引入静态背景图片;
如果服务器上存在前缀的时候: 比如 www.baidu.com/hello/你的项目, 这里的hello就是你的前缀,这时候就会出问题了,背景图片找不到了,下面踢狗两种方法。
一:打开 build 文件夹下的 utils.js
找到如下配置

在这里插入图片描述
添加 publicPath:"../../"
二:直接在组件中导入
如:import hello from "../assets/hello.png" , 并在 template 中动态导入 :style="{background: 'url('+hello+')'}" ; 这样就算服务器上有再多的前缀,也照常能够引入。
或者在 data 中
data(){
    return {
        hello:require("../assets/hello.png"),
    }
},
上一篇 下一篇

猜你喜欢

热点阅读