index.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <template>
  2. <el-icon v-if="elIcons[props.name]">
  3. <component :style="iconStyle" :is="props.name"></component>
  4. </el-icon>
  5. <svg v-else :class="svgClass" :style="iconStyle" aria-hidden="true">
  6. <use :xlink:href="symbolId" :fill="color" />
  7. </svg>
  8. </template>
  9. <script setup lang="ts" name="SvgIcon">
  10. import * as Icons from '@element-plus/icons-vue'
  11. import { CSSProperties } from 'vue'
  12. const elIcons: { [key: string]: any } = Icons
  13. interface SvgProps {
  14. name: string // 图标的名称 ==> 必传
  15. prefix?: string // 图标的前缀 ==> 非必传(默认为"icon")
  16. iconStyle?: CSSProperties // 图标的样式 ==> 非必传
  17. color?: string // 图标的样式填充颜色 ==> 非必传
  18. className?: string // 图标的class ==> 非必传
  19. }
  20. const props = withDefaults(defineProps<SvgProps>(), {
  21. prefix: 'icon',
  22. iconStyle: () => ({})
  23. })
  24. const symbolId = computed(() => `#${props.prefix}-${props.name}`)
  25. const svgClass = computed(() => {
  26. if (props.className) {
  27. return `svg-icon ${props.className}`
  28. }
  29. return 'svg-icon'
  30. })
  31. </script>
  32. <style scope lang="scss">
  33. .sub-el-icon,
  34. .nav-icon {
  35. position: relative;
  36. display: inline-block;
  37. margin-right: 12px;
  38. font-size: 15px;
  39. }
  40. .svg-icon {
  41. position: relative;
  42. width: 1em;
  43. height: 1em;
  44. margin-right: 5px;
  45. color: var(--color);
  46. fill: currentColor;
  47. }
  48. </style>