index.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { addUnit, isDef } from '../common/utils';
  2. import { VantComponent } from '../common/component';
  3. import { button } from '../mixins/button';
  4. import { openType } from '../mixins/open-type';
  5. VantComponent({
  6. mixins: [button, openType],
  7. classes: ['custom-class', 'loading-class', 'error-class', 'image-class'],
  8. props: {
  9. src: String,
  10. width: String,
  11. height: String,
  12. fit: {
  13. type: String,
  14. value: 'fill'
  15. },
  16. round: Boolean,
  17. lazyLoad: Boolean,
  18. showError: {
  19. type: Boolean,
  20. value: true
  21. },
  22. showLoading: {
  23. type: Boolean,
  24. value: true
  25. },
  26. showMenuByLongpress: Boolean,
  27. // 受小程序slot限制所需要的属性
  28. useLoadingSlot: Boolean,
  29. useErrorSlot: Boolean,
  30. },
  31. data: {
  32. fitWeapp: 'aspectFit',
  33. FIT_MODE_MAP: {
  34. contain: 'aspectFit',
  35. cover: 'aspectFill',
  36. fill: 'scaleToFill',
  37. none: 'center',
  38. // TODO: 这个没有原生的属性,需要后面实现,暂时先用contain;
  39. 'scale-down': 'aspectFit'
  40. },
  41. loading: true,
  42. error: false
  43. },
  44. watch: {
  45. src() {
  46. this.setData({
  47. loading: true,
  48. error: false
  49. });
  50. }
  51. },
  52. mounted() {
  53. this.init();
  54. },
  55. methods: {
  56. init() {
  57. const { FIT_MODE_MAP, fit } = this.data;
  58. this.setData({
  59. mode: FIT_MODE_MAP[fit],
  60. style: this.getStyle(),
  61. });
  62. },
  63. getStyle() {
  64. const { width, height } = this.data;
  65. let style = '';
  66. if (isDef(width)) {
  67. style += `width: ${addUnit(width)};`;
  68. }
  69. if (isDef(height)) {
  70. style += `height: ${addUnit(height)};`;
  71. }
  72. return style;
  73. },
  74. onLoad(event) {
  75. this.setData({
  76. loading: false
  77. });
  78. this.$emit('load', event.detail);
  79. },
  80. onError(event) {
  81. this.setData({
  82. loading: false,
  83. error: true,
  84. });
  85. this.$emit('error', event.detail);
  86. },
  87. onClick(event) {
  88. this.$emit('click', event.detail);
  89. },
  90. }
  91. });