index.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <uni-shadow-root class="vant-image-index"><view :style="computed.rootStyle({ width, height, radius })" :class="'custom-class '+(utils.bem('image', { round }))" @click="onClick">
  3. <image v-if="(!error)" :src="src" :mode="computed.mode(fit)" :lazy-load="lazyLoad" :webp="webp" 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" custom-class="van-image__loading-icon"></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="photo-fail" custom-class="van-image__error-icon"></van-icon>
  11. </view>
  12. </view></uni-shadow-root>
  13. </template>
  14. <wxs src="../wxs/utils.wxs" module="utils"></wxs><wxs src="./index.wxs" module="computed"></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 { VantComponent } from '../common/component';
  21. import { button } from '../mixins/button';
  22. VantComponent({
  23. mixins: [button],
  24. classes: ['custom-class', 'loading-class', 'error-class', 'image-class'],
  25. props: {
  26. src: {
  27. type: String,
  28. observer() {
  29. this.setData({
  30. error: false,
  31. loading: true,
  32. });
  33. },
  34. },
  35. round: Boolean,
  36. width: null,
  37. height: null,
  38. radius: null,
  39. lazyLoad: Boolean,
  40. useErrorSlot: Boolean,
  41. useLoadingSlot: Boolean,
  42. showMenuByLongpress: Boolean,
  43. fit: {
  44. type: String,
  45. value: 'fill',
  46. },
  47. webp: {
  48. type: Boolean,
  49. value: false,
  50. },
  51. showError: {
  52. type: Boolean,
  53. value: true,
  54. },
  55. showLoading: {
  56. type: Boolean,
  57. value: true,
  58. },
  59. },
  60. data: {
  61. error: false,
  62. loading: true,
  63. viewStyle: '',
  64. },
  65. methods: {
  66. onImageLoad(event) {
  67. this.setData({
  68. loading: false,
  69. });
  70. this.$emit('load', event.detail);
  71. },
  72. onImageError(event) {
  73. this.setData({
  74. loading: false,
  75. error: true,
  76. });
  77. this.$emit('error', event.detail);
  78. },
  79. onClick(event) {
  80. this.$emit('click', event.detail);
  81. },
  82. },
  83. });
  84. export default global['__wxComponents']['vant/image/index']
  85. </script>
  86. <style platform="mp-weixin">
  87. @import '../common/index.css';.van-image{display:inline-block;position:relative}.van-image--round{border-radius:50%;overflow:hidden}.van-image--round .van-image__img{border-radius:inherit}.van-image__error,.van-image__img,.van-image__loading{display:block;height:100%;width:100%}.van-image__error,.van-image__loading{align-items:center;background-color:var(--image-placeholder-background-color,#f7f8fa);color:var(--image-placeholder-text-color,#969799);display:flex;flex-direction:column;font-size:var(--image-placeholder-font-size,14px);justify-content:center;left:0;position:absolute;top:0}.van-image__loading-icon{color:var(--image-loading-icon-color,#dcdee0);font-size:var(--image-loading-icon-size,32px)!important}.van-image__error-icon{color:var(--image-error-icon-color,#dcdee0);font-size:var(--image-error-icon-size,32px)!important}
  88. </style>