1

I am trying to build a simple component in Svelte. Image component with fallback in case the src is invalid. This is my code and it doesn't work. I've tried many variations (on:error instead of onError, listen to error on mount which was sometimes too late) but none worked.
Any idea what is the right approach?

<script> import TCFavIcon from '@src/common/icons/TCFavIcon.png'; export let src = TCFavIcon; export let alt = undefined; function handleError() { console.log('error'); // This never happen even when src is invalid src = TCFavIcon; } </script> <style> </style> <img onError={handleError} {alt} {src} /> 

1 Answer 1

5

try:
<img on:error={handleError} {alt} {src} />
Svelte docs here

Sign up to request clarification or add additional context in comments.

1 Comment

any updates on that? It doesnt seem to work in svelte ^v3.5

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.