JG_GJ
JG_GJ

Reputation: 785

Why carousel does not work when using v-for in img, vuejs?

I am currently using the following library: https://www.npmjs.com/package/vue-owl-carousel.

It turns out that when using v-for in img inside the carousel, the carousel does not work, correctly.

This way it doesn't work:

<carousel
    :autoplay="true"
    :loop="true"
    :center="true"
    :nav="false"
    :margin="5"
>
  <img
    v-for="item in detIncidente.fotos"
    :key="item.fecha"
    :src="item.path"
    width="446"
    height="446"
    @click="showSingle(item.path)"
   />
</carousel>

This way if it works correctly for me:

    <carousel
      :autoplay="true"
      :loop="true"
      :center="true"
      :nav="false"
      :margin="5"
    >
       <img width="446" height="669" src="https://placeimg.com/200/200/any?3" />
       <img width="446" height="669" src="https://placeimg.com/200/200/any?4" />
       <img width="446" height="669" src="https://placeimg.com/200/200/any?2" />
       <img width="446" height="669" src="https://placeimg.com/200/200/any?3" />
</carousel>

Upvotes: 1

Views: 1726

Answers (1)

ittus
ittus

Reputation: 22403

If you load the images dynamically, the carousel component doesn't wait until data is loaded.

You can try adding v-if

v-if="detIncidente && detIncidente.fotos && detIncidente.fotos.length"

Full code

<carousel
    v-if="detIncidente && detIncidente.fotos && detIncidente.fotos.length"
    :autoplay="true"
    :loop="true"
    :center="true"
    :nav="false"
    :margin="5"
>
  <img
    v-for="item in detIncidente.fotos"
    :key="item.fecha"
    :src="item.path"
    width="446"
    height="446"
    @click="showSingle(item.path)"
   />
</carousel>

Upvotes: 4

Related Questions