.开头,会被 webpack 处理
、background: url(...) 和 CSS @import 的资源 URL 都会被解析为一个模块依赖。例如,url(./image.png)会被编译为require("./image.png")而:
<img src="./image.png">
将会被编译到:
h('img', { attrs: { src: require('./image.png') }})
. 开头,它会作为一个相对模块请求被解释且基于你的文件系统中的目录结构进行解析。

就表示引入当前组件下img目录下的28.jpg文件示例如下
src/components/HelloWorld.vue
<template>
<div class="hello">
<img src="./img/28.jpg" alt="" />
div>
template>
引入的就是src/components/img/28.jpg,参照于HelloWord.vue所在的路径,也就是以src/components为起始点,去寻找img目录下的28.jpg文件

/开头的,或是省略了/或者是.
/images/foo.png就表示是一个绝对路径在vue当中images/foo.png也表示一个绝对路径在vue当中
绝对路径的表示是以URL为/开头的,或是省略了/或者是.
比如/images/foo.png就表示是一个绝对路径在vue当中
images/foo.png也表示一个绝对路径在vue当中
转换规则很简单,绝对路径的转换规则就是以public为起始点

public/img/pic1.png文件示例代码如下
<template>
<div class="hello">
<img src="./img/28.jpg" alt="" title="相对路径的引入静态资源" />
<img src="@/components/img/28.jpg" alt="" title="url路径含有@" />
<img src="/resource/img/pic1.png" alt="" />
<img src="resource/img/pic1.png" alt="" />
div>
template>
src="/resource/img/pic1.png"和src="resource/img/pic1.png"public/img/pic1.png文件
@开头,它也会作为一个模块请求被解析,它的用户在于Vue Cli默认会设置一个执行/src 的别名@(仅作用于模板中)
@了,就说明是项目路径/src路径为起始点了src/components/HelloWorld.vue
<template>
<div class="hello">
<img src="./img/28.jpg" alt="" />
<img src="@/components/img/28.jpg" alt="" />
div>
template>
src = "./img/28.jpg"和src="@/components/img/28.jpg"都是指向src/components/img/28.jpg的图片文件
![]()
~路径URL多用于css当中node_modules中引用资源。css里面,如果使用相对路径去引入图片,那没有什么区别,都会引入src/assets/26.jpg文件
如果使用了绝对路径去引入图片,那么不添加~,就会报错

所以需要添加下~就可以
<template>
<div>
<div class="show-pic">
<img
v-for="(picPath, index) in imgList"
:key="index"
:src="picPath"
alt="123"
/>
div>
div>
template>
data() {
return {
name: "李白",
imgList: [
"./assets/46.jpg",
"./assets/47.jpg",
"./assets/48.jpg"],
};
},
<template>
<div>
<div class="show-pic">
<img
v-for="(picPath, index) in imgList"
:key="index"
:src="picPath"
alt="123"
/>
div>
div>
template>
data() {
return {
name: "李白",
imgList: [
require("./assets/46.jpg"),
require("./assets/47.jpg"),
require("./assets/48.jpg")],
};
},
vue引入静态资源有相对路径和绝对路径的方式
相对路径是URL开头有.
绝对路径是URL开头为/或者省略不写
相对路径相对的是当前文件
绝对路径相对的是public目录
并且引入的时候URL开头为~的使用在css里面引入静态资源的时候
如果 URL 以 @ 开头会作为一个模块请求被解析。Vue CLI 默认会设置一个指向 src 的别名 @ 。
https://segmentfault.com/a/1190000021485662
https://segmentfault.com/a/1190000019495695