【发布时间】:2020-03-08 05:24:13
【问题描述】:
最近,我在我的项目中使用了vue-awesome-swiper。这是我在 HomeSwiper.vue 文件中的代码:
//HTML
<template>
<swiper :options="swiperOption" ref="mySwiper">
<swiper-slide v-for="(item,index) in banners" :key="index">
<a :href="item.link">
<img :src="item.image" alt="">
</a>
</swiper-slide>
<div class="swiper-pagination" v-for="(item,index) in banners" :key="index" slot="pagination">
</div>
</swiper>
</template>
// JS
import {swiper,swiperSlide} from 'vue-awesome-swiper'
require("swiper/dist/css/swiper.css");
export default {
name:'HomeSwiper',
props:{
banners:{
type:Array,
default(){
return []
}
}
},
components:{
swiper,
swiperSlide
},
data() {
const that = this
return {
imgIndex: 1,
swiperOption: {
notNextTick: true,
loop: true,
initialSlide: 0,
autoplay: {
delay: 1500,
stopOnLastSlide: false,
disableOnInteraction: true
},
speed: 800,
direction: "horizontal",
grabCursor: true,
on: {
slideChangeTransitionStart: function() {
that.imgIndex= this.realIndex - 1;
},
},
pagination: {
el: ".swiper-pagination",
clickable: true,
type: "bullets"
}
}
}
},
computed: {
swiper() {
return this.$refs.mySwiper.swiper;
}
}
}
代码sn-p看起来正常。但是我遇到了2个奇怪的问题:
- 我发现图像无法正常循环,即使我在脚本中将循环设置为 true,当到达最后一张图像时它总是停止。
- 分页无法显示
所以我去github问题寻求帮助。遇到同样问题的人通过在swiper元素中使用v-if="banners.length!=0"来解决这些问题。但是我很困惑为什么这两个问题与横幅的长度有关?有人可以解释一下这个?
【问题讨论】: