From 4fb7bf2f9c3233bc8e7eb5e8a4495b5cb0b5b4ea Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Wed, 29 May 2024 10:54:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=8A=A0=E8=BD=BD=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=8D=A0=E4=BD=8D=E5=9B=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/LazyImage.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/components/LazyImage.js b/components/LazyImage.js index 4e353ad8..73a20f89 100644 --- a/components/LazyImage.js +++ b/components/LazyImage.js @@ -20,6 +20,8 @@ export default function LazyImage({ style }) { const maxWidth = siteConfig('IMAGE_COMPRESS_WIDTH') + const defaultPlaceholderSrc = siteConfig('IMG_LAZY_LOAD_PLACEHOLDER') + const imageRef = useRef(null) const [adjustedSrc, setAdjustedSrc] = useState( placeholderSrc || siteConfig('IMG_LAZY_LOAD_PLACEHOLDER') @@ -37,6 +39,14 @@ export default function LazyImage({ onLoad() // 触发传递的onLoad回调函数 } } + /** + * 图片加载失败回调 + */ + const handleImageError = () => { + if (imageRef.current) { + imageRef.current.src = defaultPlaceholderSrc + } + } useEffect(() => { const adjustedImageSrc = adjustImgSize(src, maxWidth) @@ -71,7 +81,8 @@ export default function LazyImage({ ref: imageRef, src: priority ? adjustedSrc : placeholderSrc, alt: alt, - onLoad: handleImageLoad + onLoad: handleImageLoad, + onError: handleImageError // 添加onError处理函数 } if (id) {