From e5a294e2585bfacafd368ab8d250f94d92dc29f9 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 31 May 2023 22:18:23 +0100 Subject: [PATCH] fix: test for `imgEl` before setting `src` resolves #783 --- src/runtime/components/nuxt-img.ts | 11 ++++++++--- src/runtime/components/nuxt-picture.ts | 6 ++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/runtime/components/nuxt-img.ts b/src/runtime/components/nuxt-img.ts index 5e8ccb364..c752b0683 100644 --- a/src/runtime/components/nuxt-img.ts +++ b/src/runtime/components/nuxt-img.ts @@ -100,12 +100,17 @@ export default defineComponent({ const img = new Image() img.src = mainSrc.value img.onload = (event) => { - imgEl.value!.src = mainSrc.value + if (imgEl.value) { + imgEl.value.src = mainSrc.value + } placeholderLoaded.value = true ctx.emit('load', event) } - } else { - imgEl.value!.onload = (event) => { + return + } + + if (imgEl.value) { + imgEl.value.onload = (event) => { ctx.emit('load', event) } } diff --git a/src/runtime/components/nuxt-picture.ts b/src/runtime/components/nuxt-picture.ts index 0c7a2489d..ee28e32ba 100644 --- a/src/runtime/components/nuxt-picture.ts +++ b/src/runtime/components/nuxt-picture.ts @@ -82,8 +82,10 @@ export default defineComponent({ } onMounted(() => { - imgEl.value!.onload = (event) => { - ctx.emit('load', event) + if (imgEl.value) { + imgEl.value.onload = (event) => { + ctx.emit('load', event) + } } })