|
@@ -0,0 +1,61 @@
|
|
|
+<!-- DynamicIcon.vue -->
|
|
|
+<template>
|
|
|
+ <el-icon>
|
|
|
+ <!-- 判断是否为 Element Plus 图标 -->
|
|
|
+ <component :is="elementPlusIcon" v-if="isElementPlusIcon" />
|
|
|
+
|
|
|
+ <!-- 判断是否为本地 SVG 图标 -->
|
|
|
+ <img v-else :src="localIconPath" :alt="alt" class="local-icon" />
|
|
|
+ </el-icon>
|
|
|
+
|
|
|
+ <!-- <div class="icon-container">-->
|
|
|
+ <!-- <!– 判断是否为 Element Plus 图标 –>-->
|
|
|
+ <!-- <component :is="elementPlusIcon" v-if="isElementPlusIcon" />-->
|
|
|
+
|
|
|
+ <!-- <!– 判断是否为本地 SVG 图标 –>-->
|
|
|
+ <!-- <img v-else :src="localIconPath" :alt="alt" class="local-icon" />-->
|
|
|
+ <!-- </div>-->
|
|
|
+</template>
|
|
|
+
|
|
|
+<script setup lang="ts">
|
|
|
+import { computed } from 'vue'
|
|
|
+
|
|
|
+interface Props {
|
|
|
+ icon: string
|
|
|
+ alt?: string
|
|
|
+}
|
|
|
+
|
|
|
+const props = defineProps<Props>()
|
|
|
+
|
|
|
+// 判断是否为 Element Plus 图标(首字母大写,且不包含斜杠或 .svg)
|
|
|
+const isElementPlusIcon = computed(() => {
|
|
|
+ return /^[A-Z]/.test(props.icon) && !props.icon.includes('/') && !props.icon.endsWith('.svg')
|
|
|
+})
|
|
|
+
|
|
|
+// 动态映射 Element Plus 图标
|
|
|
+const elementPlusIcon = computed(() => {
|
|
|
+ return props.icon
|
|
|
+})
|
|
|
+
|
|
|
+// 本地 SVG 图标路径
|
|
|
+const localIconPath = computed(() => {
|
|
|
+ // if (!isElementPlusIcon && props.icon.startsWith('@/')) {
|
|
|
+ // return new URL(`../${props.icon.replace('@/assets/', '')}`, import.meta.url).href
|
|
|
+ // }
|
|
|
+ console.log('----')
|
|
|
+ return props.icon
|
|
|
+})
|
|
|
+</script>
|
|
|
+
|
|
|
+<style scoped>
|
|
|
+/*.icon-container {*/
|
|
|
+/* display: inline-block;*/
|
|
|
+/* vertical-align: middle;*/
|
|
|
+/*}*/
|
|
|
+
|
|
|
+.local-icon {
|
|
|
+ width: 1em;
|
|
|
+ height: 1em;
|
|
|
+ object-fit: contain;
|
|
|
+}
|
|
|
+</style>
|