cIcon.vue 865 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <script setup lang="ts">
  2. import { ref, watch } from 'vue'
  3. import { getAssetsImageUrl } from '@/utils/common'
  4. import { loadResource } from '@/utils/resource'
  5. const props = withDefaults(
  6. defineProps<{
  7. src: string
  8. size?: number
  9. }>(),
  10. {
  11. size: 20,
  12. },
  13. )
  14. const iconSrc = ref<string>()
  15. watch(
  16. () => props.src,
  17. async (newSrc: string) => {
  18. const assetUrl = getAssetsImageUrl('icon' + newSrc)
  19. const blobUrl = await loadResource(assetUrl)
  20. iconSrc.value = blobUrl
  21. },
  22. { deep: true, immediate: true },
  23. )
  24. </script>
  25. <template>
  26. <div :style="{ width: `${size}px`, height: `${size}px` }">
  27. <el-image
  28. v-bind="{ ...$attrs }"
  29. :fit="$attrs['fit'] ? $attrs['fit'] : 'fill'"
  30. :src="iconSrc"
  31. class="cIcon"
  32. ></el-image>
  33. </div>
  34. </template>
  35. <style scoped>
  36. .cIcon {
  37. display: flex;
  38. align-items: center;
  39. }
  40. </style>