index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <uni-shadow-root class="vant-image-index"><view :style="viewStyle" :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. widthFix: 'widthFix',
  30. heightFix: 'heightFix',
  31. };
  32. VantComponent({
  33. mixins: [button, openType],
  34. classes: ['custom-class', 'loading-class', 'error-class', 'image-class'],
  35. props: {
  36. src: {
  37. type: String,
  38. observer() {
  39. this.setData({
  40. error: false,
  41. loading: true,
  42. });
  43. },
  44. },
  45. round: Boolean,
  46. width: {
  47. type: null,
  48. observer: 'setStyle',
  49. },
  50. height: {
  51. type: null,
  52. observer: 'setStyle',
  53. },
  54. radius: null,
  55. lazyLoad: Boolean,
  56. useErrorSlot: Boolean,
  57. useLoadingSlot: Boolean,
  58. showMenuByLongpress: Boolean,
  59. fit: {
  60. type: String,
  61. value: 'fill',
  62. observer: 'setMode',
  63. },
  64. showError: {
  65. type: Boolean,
  66. value: true,
  67. },
  68. showLoading: {
  69. type: Boolean,
  70. value: true,
  71. },
  72. },
  73. data: {
  74. error: false,
  75. loading: true,
  76. viewStyle: '',
  77. },
  78. mounted() {
  79. this.setMode();
  80. this.setStyle();
  81. },
  82. methods: {
  83. setMode() {
  84. this.setData({
  85. mode: FIT_MODE_MAP[this.data.fit],
  86. });
  87. },
  88. setStyle() {
  89. const { width, height, radius } = this.data;
  90. let style = '';
  91. if (isDef(width)) {
  92. style += `width: ${addUnit(width)};`;
  93. }
  94. if (isDef(height)) {
  95. style += `height: ${addUnit(height)};`;
  96. }
  97. if (isDef(radius)) {
  98. style += 'overflow: hidden;';
  99. style += `border-radius: ${addUnit(radius)};`;
  100. }
  101. this.setData({ viewStyle: style });
  102. },
  103. onImageLoad(event) {
  104. this.setData({
  105. loading: false,
  106. });
  107. this.$emit('load', event.detail);
  108. },
  109. onImageError(event) {
  110. this.setData({
  111. loading: false,
  112. error: true,
  113. });
  114. this.$emit('error', event.detail);
  115. },
  116. onClick(event) {
  117. this.$emit('click', event.detail);
  118. },
  119. },
  120. });
  121. export default global['__wxComponents']['vant/image/index']
  122. </script>
  123. <style platform="mp-weixin">
  124. @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)}
  125. </style>