index.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <template>
  2. <i v-if="isShowIconSvg" class="el-icon" :style="setIconSvgStyle">
  3. <component :is="getIconName" />
  4. </i>
  5. <div v-else-if="isShowIconImg" :style="setIconImgOutStyle">
  6. <img :src="getIconName" :style="setIconSvgInsStyle" />
  7. </div>
  8. <i v-else :class="getIconName" :style="setIconSvgStyle" />
  9. </template>
  10. <script setup lang="ts" name="svgIcon">
  11. import { computed } from 'vue'
  12. // 定义父组件传过来的值
  13. const props = defineProps({
  14. // svg 图标组件名字
  15. name: {
  16. type: String,
  17. },
  18. // svg 大小
  19. size: {
  20. type: Number,
  21. default: () => 14,
  22. },
  23. // svg 颜色
  24. color: {
  25. type: String,
  26. },
  27. })
  28. // 在线链接、本地引入地址前缀
  29. // https://gitee.com/lyt-top/vue-next-admin/issues/I62OVL
  30. const linesString = ['https', 'http', '/src', '/assets', 'data:image', import.meta.env.VITE_PUBLIC_PATH]
  31. // 获取 icon 图标名称
  32. const getIconName = computed(() => {
  33. return props?.name
  34. })
  35. // 用于判断 element plus 自带 svg 图标的显示、隐藏
  36. const isShowIconSvg = computed(() => {
  37. return props?.name?.startsWith('ele-')
  38. })
  39. // 用于判断在线链接、本地引入等图标显示、隐藏
  40. const isShowIconImg = computed(() => {
  41. return linesString.find((str) => props.name?.startsWith(str))
  42. })
  43. // 设置图标样式
  44. const setIconSvgStyle = computed(() => {
  45. return `font-size: ${props.size}px;color: ${props.color};`
  46. })
  47. // 设置图片样式
  48. const setIconImgOutStyle = computed(() => {
  49. return `width: ${props.size}px;height: ${props.size}px;display: inline-block;overflow: hidden;`
  50. })
  51. // 设置图片样式
  52. // https://gitee.com/lyt-top/vue-next-admin/issues/I59ND0
  53. const setIconSvgInsStyle = computed(() => {
  54. const filterStyle: string[] = []
  55. const compatibles: string[] = ['-webkit', '-ms', '-o', '-moz']
  56. compatibles.forEach((j) => filterStyle.push(`${j}-filter: drop-shadow(${props.color} ${props.size} 0);`))
  57. return `width: ${props.size}px;height: ${props.size}px;position: relative;left: -${props.size}px;${filterStyle.join('')}`
  58. })
  59. </script>