ShowImageOrNull.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <div :style="{
  3. display: 'inline-block',
  4. overflow: 'hidden',
  5. width: `${imgSize}px`,
  6. height: `${imgSize}px`,
  7. }">
  8. <a-image
  9. :src="imgUrl"
  10. :fallback="failImage"
  11. :width="imgSize"
  12. :height="imgSize"
  13. />
  14. </div>
  15. </template>
  16. <script lang="ts">
  17. import { StringUtils } from '@imengyu/imengyu-utils';
  18. import { defineComponent } from 'vue';
  19. export default defineComponent({
  20. name: "ShowImageOrNull",
  21. props: {
  22. nullImage: {
  23. type: String,
  24. default: () => require('@/assets/images/none.png'),
  25. },
  26. failImage: {
  27. type: String,
  28. default: () => require('@/assets/images/failed.svg'),
  29. },
  30. size: {
  31. type: [Number,String],
  32. default: 30,
  33. },
  34. src: {
  35. type: String,
  36. default: null,
  37. }
  38. },
  39. data() {
  40. return {
  41. imgUrl: '',
  42. }
  43. },
  44. computed: {
  45. imgSize() : number {
  46. if (typeof this.size === 'string')
  47. switch(this.size) {
  48. case 'default': return 55;
  49. case 'middle': return 45;
  50. case 'small': return 25;
  51. }
  52. return this.size as number;
  53. },
  54. },
  55. mounted() {
  56. setTimeout(() => { this.loadImage(); },100);
  57. },
  58. watch: {
  59. src() { this.loadImage(); }
  60. },
  61. methods: {
  62. loadImage() {
  63. if(StringUtils.isNullOrEmpty(this.src))
  64. this.imgUrl = this.nullImage as string;
  65. else
  66. this.imgUrl = this.src as string;
  67. },
  68. onError() {
  69. if(this.imgUrl != this.failImage)
  70. this.imgUrl = this.failImage as string;
  71. }
  72. }
  73. });
  74. </script>