index.vue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <div v-if="isColorIcon || isSvgIcon">
  3. <svg :style="setIconSVGStyle" aria-hidden="true" class="icon">
  4. <use :xlink:href="'#icon-' + name"></use>
  5. </svg>
  6. </div>
  7. <i v-else :class="getIconName" :style="setIconSvgStyle"/>
  8. </template>
  9. <script lang="ts" name="svgIcon" setup>
  10. import 'http://at.alicdn.com/t/c/font_4766628_7ekfe85jxt9.js' // 引入阿里图标库
  11. import {computed} from 'vue';
  12. // 定义父组件传过来的值
  13. const props = defineProps({
  14. name: { // svg 图标组件名字
  15. type: String,
  16. },
  17. size: { // svg 大小
  18. type: Number,
  19. default: () => 14,
  20. },
  21. color: { // svg 颜色
  22. type: String,
  23. },
  24. colorIcon: { //彩色
  25. type: Boolean,
  26. default: false,
  27. },
  28. isSvg: { // 是否是阿里图标库
  29. type: Boolean,
  30. default: true,
  31. }
  32. });
  33. const linesString = ['https', 'http', '/src', '/assets', 'data:image', import.meta.env.VITE_PUBLIC_PATH];
  34. const getIconName = computed(() => {
  35. return 'iconfont icon-' + props?.name;
  36. });
  37. // 用于判断 element plus 自带 svg 图标的显示、隐藏
  38. const isShowIconSvg = computed(() => {
  39. return props?.name?.startsWith('ele-');
  40. });
  41. // 用于判断在线链接、本地引入等图标显示、隐藏
  42. const isShowIconImg = computed(() => {
  43. return linesString.find((str) => props.name?.startsWith(str));
  44. });
  45. // 设置图标样式
  46. const setIconSvgStyle = computed(() => {
  47. return `font-size: ${props.size}px;color: ${props.color};`;
  48. });
  49. // 设置图片样式
  50. const setIconImgOutStyle = computed(() => {
  51. return `width: ${props.size}px;height: ${props.size}px;display: inline-block;overflow: hidden;`;
  52. });
  53. // 设置图片样式
  54. // https://gitee.com/lyt-top/vue-next-admin/issues/I59ND0
  55. const setIconSvgInsStyle = computed(() => {
  56. const filterStyle: string[] = [];
  57. const compatibles: string[] = ['-webkit', '-ms', '-o', '-moz'];
  58. compatibles.forEach((j) => filterStyle.push(`${j}-filter: drop-shadow(${props.color} 30px 0);`));
  59. return `width: ${props.size}px;height: ${props.size}px;position: relative;left: -${props.size}px;${filterStyle.join('')}`;
  60. });
  61. const setIconSVGStyle = computed(() => {
  62. return `width: ${props.size}px;height: ${props.size}px;display: inline-block;overflow: hidden;`;
  63. });
  64. //是否是彩色图标
  65. const isColorIcon = computed(() => {
  66. return props.colorIcon;
  67. });
  68. // 是否是阿里图标库
  69. const isSvgIcon = computed(() => {
  70. return props.isSvg;
  71. });
  72. </script>