<img src="../assets/imgs/0.jpg" alt="">
<img src="@/assets/imgs/0.jpg" alt="">
<img src="https://cdn.pixabay.com/photo/2023/04/01/21/11/tunnel-7893124_640.jpg" alt=""> <img src="https://glpla.github.io/utils/wallpaper/w0.jpg" alt="">
let src = 'https://glpla.github.io/utils/coffee/coffee0.jpg'
<img :src="src" alt="">
let :src="`https://glpla.github.io/utils${src}`"
let src = '/coffee/coffee0.jpg'
let src = '@/assets/imgs/0.jpg'
<img :src="src" alt="">
let src = '/imgs/logo.svg'
<img :src="src" alt=""/>
import src from '@/assets/logo.svg'
<img :src="src" alt=""/>
<img :src="getImageUrl(src)" alt=""/>
let src = '../imgs/10.jpg' const getImageUrl = (img) => { return new URL(img, import.meta.url).href }
let src = '10.jpg' const getImageUrl = (img) => { return new URL(`../imgs/${img}`, import.meta.url).href }
<div style="background-image: url('/imgs/bg.jpg');">hi,there</div> <div style="background-image: url('@/assets/bg.png');">hi,there</div>// x <div style="background-image: url(./assets/bg.jpg)">hi,there</div>// x
.bg-img { background-image: url('@/assets/dec0.png'); }
<div :style="{ 'backgroundImage': 'url(' + url + ')' }">hi,there</div> <div :style="{ 'background-image': 'url(' + url + ')' }">hi,there</div> <div :style="`background-image:url(${url})`">hi,there</div> <div :style="`backgroundImage:url(${url})`">hi,there</div> // x
<div>public</div> <img src="/coffee1.jpg" alt="" /> <div>public → imgs</div> <img src="/imgs/coffee3.jpg" alt="" /> <div>src → assets</div> <img src="../assets/coffee0.jpg" alt="" /> <div>src → assets → imgs</div> <img src="@/assets/imgs/coffee2.jpg" alt="" /> <div>src → components</div> <img src="./coffee4.jpg" alt="" /> <div>src → assets(icon)</div> <img src="@/assets/home_bar.png" alt="" />
本质上是从根路径引用,所以也可以从 / 开始,引入任意地方的资源;但是打包的时候,会出现问题,因为打包后没有 src 目录
<img v-for="img in imgs" :key="img.id" :src="`/src/assets/${img.src}`"/>
采用部分地址拼接服务器地址
<img v-for="img in imgs" :key="img.id" :src="`https://url/${img.src}`"/>