项目目录结构
./
├── assets
│ └── logo.png
└── components
└── test.vue
test.vue中的css样式,需要引用assets下的logo.png文件。
#main {
width: 200px;
height: 200px;
background: url('logo.png');
}
运行后报错:
ERROR Failed to compile with 1 errors 15:43:10
This relative module was not found:
* ./logo.png in ./node_modules/css-loader?{"sourceMap":true}!...
使用相对路径
#main {
width: 200px;
height: 200px;
background: url('../assets/logo.png');
}
注意:用多少个../ 取决于vue文件和assets目录之间,跨越了多少层级。
我这里只用了一个,如果你的层级比较深,需要添加多个../
完整代码如下: test.vue
<template>
<div id="main"></div>
</template>
<script>
export default {
name: "test"
}
</script>
<style scoped>
#main {
width: 200px;
height: 200px;
background: url('../assets/logo.png');
}
</style>
访问页面,效果如下:
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有