image-view.vue 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <template>
  2. <image
  3. class="image"
  4. :src="imageUrl"
  5. mode="aspectFit"
  6. @load="onImageLoad"
  7. :style="{ width: styleObj.width, height: styleObj.height }"
  8. ></image>
  9. </template>
  10. <script>
  11. import { mapGetters } from 'vuex'
  12. export default {
  13. data() {
  14. return {
  15. imageUrl: '',
  16. detail: null
  17. }
  18. },
  19. computed: {
  20. ...mapGetters(['systemInfo']),
  21. styleObj() {
  22. if (!this.detail) return {}
  23. const { width, height } = this.detail
  24. const { screenWidth } = this.systemInfo
  25. let w = width
  26. let h = height
  27. if (width > screenWidth) {
  28. w = screenWidth
  29. h = (height * screenWidth) / width
  30. }
  31. const style = { width: w + 'px', height: h + 'px' }
  32. return style
  33. }
  34. },
  35. onLoad(options) {
  36. console.log(this.systemInfo)
  37. this.imageUrl = options.imageUrl && decodeURIComponent(options.imageUrl)
  38. },
  39. methods: {
  40. onImageLoad(e) {
  41. console.log(e)
  42. this.detail = e.detail
  43. }
  44. }
  45. }
  46. </script>
  47. <style scoped>
  48. .image {
  49. display: block;
  50. margin: 0 auto;
  51. }
  52. </style>