<div class="swiper-wrapper">
<div
class="swiper-slide"
v-for="(carousel, index) in bannerList"
:key="carousel.id"
>
<img :src="carousel.imgUrl" />
</div>
- 通过watch监听bannerList属性的属性值变化
- 如果执行handler方法,代表组件实例身上这个属性已经有了数组
- 当这个函数执行只能保证bannerList数据有了,无法保证v-for执行结束
- v-for结束后才有结构,所以无法显示轮播图
watch: {
bannerList: {
handler(newValue, oldValue) {
this.$nextTick(() => {
var mySwiper = new Swiper(".swiper-container", {
loop: true, // 循环模式选项
// 如果需要分页器
pagination: {
el: ".swiper-pagination",
clickable: true,
},
// 如果需要前进后退按钮
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev",
},
});
});
},
},
},
![](https://img-blog.csdnimg.cn/b51ab6b627294cb3b237cd1f18f7cd20.jpeg)
![](https://img-blog.csdnimg.cn/7b3f7939f9434ecdb6a8a159c9101e91.jpeg)
- 在下次DOM更新循环结束之后执行延迟回调。在修改数据之后立即使用这个方法,获取更新后的DOM。
- 可以保证在页面的结构一定是有的,经常和很多插件一起使用。