1、发现是 在vite中不能使用require引入图片资源,因为这里的require是webpack提供的一种加载能力,由于我们使用的是Vite,因此这里必须转用 Vite提供的静态资源载入
解决方案一:
官方例子
const imgUrl = new URL('./img.png', import.meta.url).href document.getElementById('hero-img').src = imgUrl
解决方案二:
使用import方案代替
import img from '路径地址'
解决方案三:
适合在动态数组中赋值
function getImageUrl(name){
return new URL(./style/img/${name}
,import.meta.url).href;
};
getImageUrl('a.png')