为什么在img、vuejs中使用v-for时轮播不工作?
Why carousel does not work when using v-for in img, vuejs?
我目前正在使用以下库:https://www.npmjs.com/package/vue-owl-carousel。
事实证明,当在轮播内的 img 中使用 v-for 时,轮播无法正常工作。
这种方式不行:
<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>
这种方式如果对我来说是正确的:
<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>
如果动态加载图片,轮播组件不会等到数据加载完毕。
您可以尝试添加 v-if
v-if="detIncidente && detIncidente.fotos && detIncidente.fotos.length"
完整代码
<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>
我目前正在使用以下库:https://www.npmjs.com/package/vue-owl-carousel。
事实证明,当在轮播内的 img 中使用 v-for 时,轮播无法正常工作。
这种方式不行:
<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>
这种方式如果对我来说是正确的:
<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>
如果动态加载图片,轮播组件不会等到数据加载完毕。
您可以尝试添加 v-if
v-if="detIncidente && detIncidente.fotos && detIncidente.fotos.length"
完整代码
<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>