使用绝对路径

在vue-cli项目中的放在public目录下的资源会被直接复制,不会经过webpack的打包处理。目录如下
在这里插入图片描述

我们可以在css文件中这么用

#header {
	position:relative;
	height:72px;
	background:url("/image/logo.png");
	overflow:hidden;
}

文件打包生成以后,他访问的是这个地址
http://localhost:8080/image/logo.png

相对路径

不建议使用相对路径,可能会引起各种问题

#header {
	position:relative;
	height:72px;
	background:url("../../assets/logo.png");
	overflow:hidden;
}

访问项目,图片路径是http://localhost:8080/img/logo.82b9c7a5.png

使用服务器上面的路径

目录结构如下
在这里插入图片描述

我们可以通过less或者sass,访问服务器上面的路径,将绝对路径中修改一下,以便模拟

在common.less里面

/*
css 加载图片的路径 调用方式 => url('@{cssHost}doctor-bg.png')  
现在为本地地址
*/ 
@cssHost :'http://localhost:8080/image/'; 

在dashboard.less中

@charset "utf-8";
@import "../../assets/common.less";
* {margin:0;padding:0;box-sizing:border-box;}
.dash {
	position:relative;
	font-family:"Microsoft Yahei", Arial, sans-serif;
    background-image:url("@{cssHost}dashboard/bg.png");
}
Logo

前往低代码交流专区

更多推荐