index.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <template>
  2. <el-image :src="`${realSrc}`" fit="cover" :style="`width:${realWidth};height:${realHeight};`" :preview-src-list="realSrcList" preview-teleported>
  3. <template #error>
  4. <div class="image-slot">
  5. <el-icon><picture-filled /></el-icon>
  6. </div>
  7. </template>
  8. </el-image>
  9. </template>
  10. <script setup lang="ts" name="ImgPreview">
  11. import { computed } from 'vue'
  12. interface ImgPreviewProps {
  13. src: string
  14. height?: string | number // 组件高度 ==> 非必传(默认为 150px)
  15. width?: string | number // 组件宽度 ==> 非必传(默认为 150px)
  16. }
  17. const props = withDefaults(defineProps<ImgPreviewProps>(), {
  18. src: '',
  19. height: '150px',
  20. width: '150px'
  21. })
  22. const realSrc = computed(() => {
  23. if (!props.src) {
  24. return
  25. }
  26. let real_src = props.src.split(',')[0]
  27. return real_src
  28. })
  29. const realSrcList = computed(() => {
  30. if (!props.src) {
  31. return []
  32. }
  33. let real_src_list = props.src.split(',')
  34. let srcList: string[] = []
  35. real_src_list.forEach((item: string) => {
  36. return srcList.push(item)
  37. })
  38. return srcList
  39. })
  40. const realWidth = computed(() => (typeof props.width == 'string' ? props.width : `${props.width}px`))
  41. const realHeight = computed(() => (typeof props.height == 'string' ? props.height : `${props.height}px`))
  42. </script>
  43. <style lang="scss" scoped>
  44. .el-image {
  45. background-color: #ebeef5;
  46. border-radius: 5px;
  47. box-shadow: 0 0 5px 1px #cccccc;
  48. :deep(.el-image__inner) {
  49. cursor: pointer;
  50. transition: all 0.3s;
  51. &:hover {
  52. transform: scale(1.2);
  53. }
  54. }
  55. :deep(.image-slot) {
  56. display: flex;
  57. align-items: center;
  58. justify-content: center;
  59. width: 100%;
  60. height: 100%;
  61. font-size: 30px;
  62. color: #909399;
  63. }
  64. }
  65. </style>