Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Zahid Jabbar
Zahid Jabbar

Posted on • Edited on

     

Image Binding Error Handling in Vue.js

🐛 We all know that broken images don't look good and they give a very bad user experience.

 Looks good?  Definitely no!

👉 I was working on a project and instead of showing an alt text if the image is broken, I wanted to show some other alternate image. For this, first, I needed to check if the image is broken then show the alternate image.

👀 I looked for solutions and some people were doing it by styling broken images which is also good but this was not the solution I was looking for.

🙌 I came to know about@error event on images and this proved really helpful.

👨‍💻 Let's explore this with a code example:

😕 without@error

If we don't use@error and just provide the alt text, it looks ugly.

<template><imgclass="image":src="source"alt="mountains"></template>
Enter fullscreen modeExit fullscreen mode

mountains

😍 with@error

By using the@error event on images, we can show the alternate image if the original image is broken or not loaded for any reason.@error will only call the provided method if there is any error in loading the original image.

<template><imgclass="image":src="source"@error="setAltImg"alt="mountains"></template>
Enter fullscreen modeExit fullscreen mode

imgUrlAlt is the method that sets the image's src to alternate image.

setAltImg(event){event.target.src="working-image.jpg"}
Enter fullscreen modeExit fullscreen mode

Yasss loaded

💯 Conclusion

I really liked achieving the same thing in 3-5 lines with@error and avoiding writing extra 10-15 lines of CSS.

👋 Say Hi

I am quite active onTwitter you can follow me there or visit myBlog to know more about me.

Top comments(4)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
jackwkinsey profile image
Jack Kinsey
I am a full stack web and game developer. I am a husband and father. I enjoy music, games, reading, and podcasts.Senior Software Engineer at Of Ash and FireFounder at Hexbit Studios
  • Location
    Fort Wayne, IN
  • Education
    B.S. in Computer Science
  • Work
    Senior Software Engineer
  • Joined

I had recently found this solution too, but I noticed that theonerror attribute forimg is deprecated according to MDN. My understanding is that@error boils down toonerror.

Still looking for an alternative though.

CollapseExpand
 
zahidjabbar profile image
Zahid Jabbar
🎯Loves JavaScript, books, and sometime movies

@error worked fine for me. Let me know if I can help you to solve your problem.

CollapseExpand
 
jackwkinsey profile image
Jack Kinsey
I am a full stack web and game developer. I am a husband and father. I enjoy music, games, reading, and podcasts.Senior Software Engineer at Of Ash and FireFounder at Hexbit Studios
  • Location
    Fort Wayne, IN
  • Education
    B.S. in Computer Science
  • Work
    Senior Software Engineer
  • Joined
• Edited on• Edited

Yeah we decided to go with this as it seems to work just fine and doesn't actually map directly to theonerror attribute. So all good it seems 😀

CollapseExpand
 
ricky11 profile image
Rishi U
Junior SWE at Sinosend.com. I like all things shiny and all things serverless.
  • Location
    Hong Kong
  • Work
    Almost full stack developer at Sinosend
  • Joined

Are you reaching in to the dom with event.target.src insteda of use the vue way which is this.$refs ? not sure which way is more performant if this is in a list?

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

🎯Loves JavaScript, books, and sometime movies
  • Education
    B.Sc Computer Science
  • Joined

More fromZahid Jabbar

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp