index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <uni-shadow-root class="vant-image-index"><view :style="style" :class="'custom-class '+(utils.bem('image', { round }))" @click="onClick">
  3. <image v-if="(!error)" :src="src" :mode="mode" :lazy-load="lazyLoad" class="image-class van-image__img" :show-menu-by-longpress="showMenuByLongpress" @load="onImageLoad" @error="onImageError"></image>
  4. <view v-if="loading && showLoading" class="loading-class van-image__loading">
  5. <slot v-if="useLoadingSlot" name="loading"></slot>
  6. <van-icon v-else name="photo-o" size="22"></van-icon>
  7. </view>
  8. <view v-if="error && showError" class="error-class van-image__error">
  9. <slot v-if="useErrorSlot" name="error"></slot>
  10. <van-icon v-else name="warning-o" size="22"></van-icon>
  11. </view>
  12. </view></uni-shadow-root>
  13. </template>
  14. <wxs src="../wxs/utils.wxs" module="utils"></wxs>
  15. <script>
  16. import VanIcon from '../icon/index.vue'
  17. import VanLoading from '../loading/index.vue'
  18. global['__wxVueOptions'] = {components:{'van-icon': VanIcon,'van-loading': VanLoading}}
  19. global['__wxRoute'] = 'vant/image/index'
  20. import { addUnit, isDef } from '../common/utils';
  21. import { VantComponent } from '../common/component';
  22. import { button } from '../mixins/button';
  23. import { openType } from '../mixins/open-type';
  24. const FIT_MODE_MAP = {
  25. none: 'center',
  26. fill: 'scaleToFill',
  27. cover: 'aspectFill',
  28. contain: 'aspectFit'
  29. };
  30. VantComponent({
  31. mixins: [button, openType],
  32. classes: ['custom-class', 'loading-class', 'error-class', 'image-class'],
  33. props: {
  34. src: String,
  35. round: Boolean,
  36. width: {
  37. type: null,
  38. observer: 'setStyle'
  39. },
  40. height: {
  41. type: null,
  42. observer: 'setStyle'
  43. },
  44. radius: null,
  45. lazyLoad: Boolean,
  46. useErrorSlot: Boolean,
  47. useLoadingSlot: Boolean,
  48. showMenuByLongpress: Boolean,
  49. fit: {
  50. type: String,
  51. value: 'fill',
  52. observer: 'setMode'
  53. },
  54. showError: {
  55. type: Boolean,
  56. value: true
  57. },
  58. showLoading: {
  59. type: Boolean,
  60. value: true
  61. }
  62. },
  63. data: {
  64. error: false,
  65. loading: true
  66. },
  67. watch: {
  68. src() {
  69. this.setData({
  70. error: false,
  71. loading: true
  72. });
  73. }
  74. },
  75. mounted() {
  76. this.setMode();
  77. this.setStyle();
  78. },
  79. methods: {
  80. setMode() {
  81. this.setData({
  82. mode: FIT_MODE_MAP[this.data.fit],
  83. });
  84. },
  85. setStyle() {
  86. const { width, height, radius } = this.data;
  87. let style = '';
  88. if (isDef(width)) {
  89. style += `width: ${addUnit(width)};`;
  90. }
  91. if (isDef(height)) {
  92. style += `height: ${addUnit(height)};`;
  93. }
  94. if (isDef(radius)) {
  95. style += 'overflow: hidden;';
  96. style += `border-radius: ${addUnit(radius)};`;
  97. }
  98. this.setData({ style });
  99. },
  100. onImageLoad(event) {
  101. this.setData({
  102. loading: false
  103. });
  104. this.$emit('load', event.detail);
  105. },
  106. onImageError(event) {
  107. this.setData({
  108. loading: false,
  109. error: true
  110. });
  111. this.$emit('error', event.detail);
  112. },
  113. onClick(event) {
  114. this.$emit('click', event.detail);
  115. }
  116. }
  117. });
  118. export default global['__wxComponents']['vant/image/index']
  119. </script>
  120. <style platform="mp-weixin">
  121. @import '../common/index.css';.van-image{position:relative;display:inline-block}.van-image--round{overflow:hidden;border-radius:50%}.van-image--round .van-image__img{border-radius:inherit}.van-image__error,.van-image__img,.van-image__loading{display:block;width:100%;height:100%}.van-image__error,.van-image__loading{position:absolute;top:0;left:0;display:-webkit-flex;display:flex;-webkit-flex-direction:column;flex-direction:column;-webkit-align-items:center;align-items:center;-webkit-justify-content:center;justify-content:center;color:#969799;color:var(--image-placeholder-text-color,#969799);font-size:14px;font-size:var(--image-placeholder-font-size,14px);background-color:#f7f8fa;background-color:var(--image-placeholder-background-color,#f7f8fa)}
  122. </style>