1.错误示例
<img :src='imgurl' alt=''>
const imgurl = ref('src/asset/image/xxx.png')
//绑定绝对路由虽然可以显示,但是打包后图片路径找不到
const imgurl = ref('@/asset/image/xxx.png')
//相对路由开发环境下都不会显示
2.正确做法
//给src绑定一个函数,然后把需要图片名字传给函数。
<img :src="getImageUrl(path)" alt="" />
<script setup>
const path = 'xxx.png'
//可以得到完整连接
getImageUrl = (path) => {
return new URL(`@/assets/img/${path}`, import.meta.url).href;
}
</script>