How to Treat 404 Image Error in Vuejs?

Asked

Viewed 205 times

0

I’m using the vuetifyjs Carousel,I’m consuming an api and it works well but when comes an image that does not exist or broken, it is giving error:

inserir a descrição da imagem aqui

How do I make it to leave a standard image for when it is wrong and stop giving that error on the console?

        <v-carousel class="carrosel" xs6>
          <v-carousel-item
            v-for="(item, i) in p.images"
            :key="i"
            :src="item"
            aspect-ratio="5"
          ></v-carousel-item>
        </v-carousel>

1 answer

0

Use the attribute @error that will invoke a function when giving an error in the image

new Vue({
  el: "#app",
  methods: {
  	errorImage( event ){
    
    	event.target.src = 'https://user-images.githubusercontent.com/24848110/33519396-7e56363c-d79d-11e7-969b-09782f5ccbab.png';
        console.clear();
      
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  
  <img :src="'http://www.naoexiste.com'" @error="errorImage" alt="">
  <img :src="'https://upload.wikimedia.org/wikipedia/commons/thumb/9/99/Unofficial_JavaScript_logo_2.svg/200px-Unofficial_JavaScript_logo_2.svg.png'" @error="errorImage" alt="">

  
</div>

Regarding the error in the console is a default behavior of the browser, so the only thing you can do is clean with console.clear();

  • Thank you for the answer but unfortunately it did not work, keep pointing the error and does not load the error image. I had tried something similar to this solution that also did not work. I don’t know how to get rid of this error and deal with a broken image pattern..

  • In the example above you can see working, just wait a little while the fallback image will appear (I don’t know why it takes so long), and the console.clear() clears the error, but so, is this functionality really necessary? pq this can be easily validated on the back with a get_headers(PHP) or similar methods in other languages.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.