utils/getAssetsImage.ts

1
2
3
4
5
6
7
8
9
10
11
12
// 动态获取图片
export function getAssetsImage(src: string | undefined): string {
if (!src) return ""
const assets = import.meta.glob("~/assets/image/**/*", { eager: true, import: "default" })
return assets[`/assets/image/${src}`] as string
}

export function getAssetsIcon(src: string | undefined): string {
if (!src) return ""
const assets = import.meta.glob("~/assets/icons/**/*", { eager: true, import: "default" })
return assets[`/assets/icons/${src}`] as string
}

使用

1
2
3
4
5
6
7
<script setup>
import getAssetsImage from "~/utils/getAssetsImage"
</script>

<template>
<img :src="getAssetsImage(card.bg)" alt="" />
</template>