看了vite官方文档 静态资源处理 | Vite 官方中文文档
vite 官方推荐的静态处理方式使用new URL(url, import.meta.url)
这种方式在uniapp 上,只能在H5使用,小程序并不兼容
function getImageUrl(name) {
return new URL(`./dir/${name}.png`, import.meta.url).href
}
后续发现可以使用import.meta.globEager批量引入静态资源
export function getStaticImage(path: string) {
const fullPath = `/src/static/images/${path}`;
const modules = import.meta.globEager('/src/static/images/**/*');
return modules[fullPath].default;
}
使用
<image :src="getStaticImage(`home/${name}.png`)" />