import homeIcon from '@/assets/images/home/home_icon.png'![]()
vite官网的静态资源引入参考地址
new URL() + import.meta.url
const getAssetsFile = (url) => {
return new URL(`../assets/images/${url}`, import.meta.url).href
}
注意:这里只能通过 …/…/ 这种方式去获取路径,无法通过@/assets
/**
* @description: 动态加载图片 (注意:将图片放到public目录下)
* @param {*} imgUrl public目录下图片的地址:eg: /public/imgs/a.png, 则imgUrl为 ./imgs/a.png
* @return {*} 返回图片的绝对路径
*/
const loadPicture = (imgUrl) => {
let pathnameArr = location.pathname.split("/");
let realPathArr = []
pathnameArr.forEach(item =>{
if( item && item.slice(-5) !== '.html'){
realPathArr.push(item)
}
})
let realPath = location.origin + "/"
if(realPathArr.length > 0){
realPath = realPath + realPathArr.join('/') + "/"
}
return new URL(imgUrl, realPath).href;
}
上一篇:vue组件:列表自动无限循环