index.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <template>
  2. <view class="hc-image-view" :style="{'background-image': srcs}">
  3. <image class="image-bar" lazy-load :src="url" @error="imgError" @load="imgLoad"/>
  4. <view class="bg-gray-1 hc-flex-center h-full radius" v-if="!isImage">
  5. <view class="p-1 text-gray-4">
  6. <view class="text-40 text-center">
  7. <text class="i-ri-emotion-sad-line"/>
  8. </view>
  9. <view class="text-22">加载失败</view>
  10. </view>
  11. </view>
  12. </view>
  13. </template>
  14. <script setup>
  15. import {ref, watch} from "vue";
  16. const props = defineProps({
  17. 'src': String
  18. });
  19. //图片Url
  20. const url = ref(props.src);
  21. const srcs = ref('');
  22. const isImage = ref(true)
  23. //监听变化
  24. watch(() => props.src, (src) => {
  25. url.value = src
  26. })
  27. //图片加载失败
  28. const imgError = () => {
  29. srcs.value = '';
  30. isImage.value = false
  31. }
  32. //图片载入完毕
  33. const imgLoad = () => {
  34. srcs.value = `url(${url.value})`;
  35. isImage.value = true
  36. }
  37. </script>
  38. <style lang="scss" scoped>
  39. .hc-image-view {
  40. position: relative;
  41. width: 100%;
  42. height: 100%;
  43. background-position: 50%;
  44. background-size: 50%;
  45. background-repeat: no-repeat;
  46. .image-bar {
  47. position: absolute;
  48. width: 0;
  49. height: 0;
  50. will-change: transform
  51. }
  52. }
  53. </style>