当前位置: 首页 > news >正文

Vue3封装公共图片组件

在这里插入图片描述

对图片加载做的处理:

1.图片加载状态响应式管理
2.图片访问错误的处理机制
3.图片懒加载
4.可通过slot支持自定义加载动画
5.其他监听事件的处理及向上传递

<!-- components/CustomImage.vue -->
<template><div class="custom-image-wrapper"><!-- 主图 --><img v-show="!showDefault" :src="effectiveSrc" class="img-style" :alt="altText" @error="handleError" @load="handleLoad" lazy-load="true" /><!-- 默认图 --><img v-show="showDefault" :src="effectiveDefaultSrc" class="img-style" :alt="altText" lazy-load="true" /><!-- 加载状态(可选) --><div v-if="showLoading" class="loading-overlay"><slot name="loading"><div class="loading-spinner"></div></slot></div></div>
</template><script setup>
const props = defineProps({src: {type: String,required: true},defaultSrc: {type: String,default: '../../static/404.jpg'},alt: {type: String,default: 'image'},lazy: {type: Boolean,default: true},imageClass: {type: String,default: ''},retryCount: {type: Number,default: 0},retryDelay: {type: Number,default: 2000}
});const emit = defineEmits(['error', 'loaded']);// 响应式状态
const hasError = ref(false);
const isLoading = ref(true);
const currentRetry = ref(0);
const localDefaultSrc = ref(props.defaultSrc);const altText = ref('');// 计算属性
const effectiveSrc = computed(() => {if (!props.src || hasError.value) return '';return props.src;
});const effectiveDefaultSrc = computed(() => {return localDefaultSrc.value || props.defaultSrc;
});const showDefault = computed(() => {return hasError.value || !props.src;
});const showLoading = computed(() => {return isLoading.value && !showDefault.value;
});// 处理方法
const handleError = (e) => {if (currentRetry.value < props.retryCount) {currentRetry.value++;setTimeout(() => {hasError.value = false;}, props.retryDelay);return;}hasError.value = true;isLoading.value = false;emit('error', e);
};const handleLoad = () => {isLoading.value = false;emit('loaded');
};// 监听props变化
watch(() => props.src,(newVal) => {if (newVal) {hasError.value = false;isLoading.value = true;currentRetry.value = 0;}}
);watch(() => props.defaultSrc,(newVal) => {localDefaultSrc.value = newVal;}
);
</script><style scoped>
.custom-image-wrapper {position: relative;/*  width: 100%;height: 100%; */
}img {width: 100%;height: 100%;object-fit: cover;transition: opacity 0.3s;border-radius: inherit;
}.img-style {width: var(--img-width);height: var(--img-height);border-radius: var(--img-border-radius);
}.loading-overlay {position: absolute;top: 0;left: 0;width: 100%;height: 100%;background: rgba(255, 255, 255, 0.8);display: flex;align-items: center;justify-content: center;
}.loading-spinner {width: 24px;height: 24px;border: 3px solid #ddd;border-top-color: #888;border-radius: 50%;animation: spin 1s linear infinite;
}@keyframes spin {to {transform: rotate(360deg);}
}
</style>

组件使用

<ImageView class="custom-style" :src="item.courseCover" 
default-src="../../static/404.jpg"></ImageView>
   .custom-style {--img-width: 298rpx;--img-height: 186rpx;--img-border-radius: 8rpx;}

说明:
1.图片样式直接在组件中通过给自定义组件上设置类名,在style中写CSS样式就行;
2.需要注意的是:在自定义组件中由于为了同时兼容H5、小程序,有这么一个变量穿透的类名 .img-style,如果只面向WEB端,可注释其CSS,在父组件中直接以普通形式设置图片的样式。

如果遇到样式不生效看这篇文章

http://www.xdnf.cn/news/490555.html

相关文章:

  • 深入探索 OpenSPG:下一代知识图谱构建与推理框架
  • Java(基础) day01 初识Java
  • 职教实训室中的写实数字人:技术与应用方案
  • 遇到Linux系统网络连接丢包的问题如何解决?
  • 54. 螺旋矩阵
  • redis缓存实战
  • 地球系统模式(CESM)实践技术应用
  • Ubuntu系统安装docker仓库教程
  • C#学习教程(附电子书资料)
  • Excel MCP: 自动读取、提炼、分析Excel数据并生成可视化图表和分析报告
  • day 25
  • Vue 2.0学习
  • 播放进度条小组件
  • 如何借助iPaaS集成平台做好API 版本管理
  • 记录一次vue项目页面内嵌iframe页面实现跨域上传和下载附件的功能
  • PT2031K单触控单输出触摸IC
  • UI自动化测试中,一个完整的断言应所需要考虑的问题
  • 基于SpringBoot的房屋租赁管理系统
  • 有什么软件系统可以高效管理工地现场物资材料?
  • C语言—指针4
  • 【Manim】使用manim画一个高斯分布的动画
  • Java【13_2】多态、根父类
  • 【已解决】Parsing error: No Babel config file detected for E:\
  • MCP概述及MCP Server的使用和实现(谷歌ADK使用MCP Server)
  • 如何在 Windows 上安装类似 Synaptic 的 Chocolatey GUI 包管理器
  • 哈希表的实现02
  • java18
  • 理解位图算法:使用 C++ 实现高效数据查重
  • 4.1 多层感知机 MLP 笔记
  • C语言学习记录--深入理解指针(5)(qsort的练习)