vite静态资源处理

/**
 * 获取完整解析静态资源的url,如图片、svg等
 * @param {string} path 静态资源在在assets中的地址,例如:images/pubilc/logo.png
 * @returns {string}
 */
export function getStaticUrl(path: string | undefined): string {
  if (typeof path == "undefined") return "";
  let paths = path.split("/");
  // 去掉空值或者.或者..
  paths = paths.filter((item) => item !== "" && item !== "." && item !== "..");
  return new URL(`../assets/${paths.join("/")}`, import.meta.url).href;
}
posted @ 2025-09-23 22:15  小周同学~  阅读(8)  评论(0)    收藏  举报