S
S
Sport-code2019-07-18 14:08:28
Vue.js
Sport-code, 2019-07-18 14:08:28

How to handle v-img error?

Hello!
How can I catch the 404 error (if there is no image), instead show the image that the image is not loaded?

v-for="(product, index) in currentPageItems"
    :key="index"
  >
     <div class="px-2 pt-2">
           <v-img v-if = "image === true"
                 :src="$hostname + `/uploads/images/goods/${product.imageSrc}`"
                 height="200px" 
                 contain 
                 position='center'
                 v-on:error="onImgError"
            />
           <v-img v-else 
                 class="mt-3 grey light-2" 
                 :src="$hostname + `/uploads/images/nopic.png`"
                 height="200px" 
                 contain 
                 position='center' 
           />
    </div>

Please tell me if anyone knows.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
0
0xD34F, 2019-07-20
@Sport-code

Add a Boolean property to currentPageItems to indicate if there is an error. Assign src depending on the value of this property. Set it to true when handling the error event.
https://jsfiddle.net/vuz756br/

G
genius_spirit, 2019-07-18
@genius_spirit

v-if = "product.imageSrc"
or
class="mt-3 gray light-2"
:src="product.imageSrc ? `${hostname} + /uploads/images/goods/${product.imageSrc}` : `${hostname} + /uploads/images/nopic.png`"
height="200px"
contain
position='center'
/>

E
Evgeniy S, 2019-07-18
@evgensenin

Onerror
https://learn.javascript.ru/onload-onerror#zagruzk...
can catch the image loading error , if this is not in the v-img component, then you will have to sculpt your own.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question