index.vue 777 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <template>
  2. <svg :class="svgClass" aria-hidden="true" v-on="$attrs">
  3. <use :xlink:href="iconName" />
  4. </svg>
  5. </template>
  6. <script setup lang="ts" name="SvgIcon">
  7. import { PropType, computed } from 'vue'
  8. const props = defineProps({
  9. // svg文件名
  10. iconClass: {
  11. type: String as PropType<string>,
  12. required: true
  13. },
  14. // 自定义类名
  15. className: {
  16. type: String as PropType<string>,
  17. default: ''
  18. }
  19. })
  20. const iconName = computed(() => `#icon-${props.iconClass}`)
  21. const svgClass = computed(() => {
  22. if (props.className) {
  23. return `svg-icon ${props.className}`
  24. } else {
  25. return 'svg-icon'
  26. }
  27. })
  28. </script>
  29. <style scoped>
  30. .svg-icon {
  31. width: 1em;
  32. height: 1em;
  33. overflow: hidden;
  34. vertical-align: -0.15em;
  35. fill: currentColor;
  36. }
  37. </style>