index.vue 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <uni-shadow-root class="vant-dist-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" 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/dist/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. showError: {
  48. type: Boolean,
  49. value: true,
  50. },
  51. showLoading: {
  52. type: Boolean,
  53. value: true,
  54. },
  55. },
  56. data: {
  57. error: false,
  58. loading: true,
  59. viewStyle: '',
  60. },
  61. methods: {
  62. onImageLoad(event) {
  63. this.setData({
  64. loading: false,
  65. });
  66. this.$emit('load', event.detail);
  67. },
  68. onImageError(event) {
  69. this.setData({
  70. loading: false,
  71. error: true,
  72. });
  73. this.$emit('error', event.detail);
  74. },
  75. onClick(event) {
  76. this.$emit('click', event.detail);
  77. },
  78. },
  79. });
  80. export default global['__wxComponents']['vant/dist/image/index']
  81. </script>
  82. <style platform="mp-weixin">
  83. @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:flex;flex-direction:column;align-items: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)}.van-image__loading-icon{color:#dcdee0;color:var(--image-loading-icon-color,#dcdee0);font-size:32px!important;font-size:var(--image-loading-icon-size,32px)!important}.van-image__error-icon{color:#dcdee0;color:var(--image-error-icon-color,#dcdee0);font-size:32px!important;font-size:var(--image-error-icon-size,32px)!important}
  84. </style>