vue.js - 如何正常制作 vue-awesome-swiper 循环并显示分页?
问题描述
最近,我在我的项目中使用了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;
}
}
}
代码片段看起来很正常。但是我遇到了两个奇怪的问题:
- 我发现图像无法正常循环,即使我在脚本中将循环设置为 true,当到达最后一张图像时它总是停止。
- 分页无法显示
所以我去github问题寻求帮助。遇到同样问题的人通过v-if="banners.length!=0"
在swiper元素中使用解决了这些问题。但我很困惑为什么这两个问题与横幅的长度有关?有人可以解释一下吗?
解决方案
您似乎缺少<v-content>
视图中的标签。
您的整个视图应该用 a 包裹<v-content>
以确保正确调整大小。您可以在此处查看文档:https ://vuetifyjs.com/en/components/application/
模板:
<template>
<v-content>
<Swiper :options="swiperOption">
<Swiper-Slide v-for="(item,index) in srcs" :key="index">
<img class="slideImage" :src="require(`@/static/vuetifypro-pages/${ item }`)" :key="index" alt="">
</Swiper-Slide>
<div class="swiper-pagination swiper-pagination-white" slot="pagination"></div>
</Swiper>
</v-content>
</template>
脚本:
<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper';
export default {
name:'HomeSwiper',
components:{
Swiper,
SwiperSlide
},
data() {
return {
srcs: {
1: 'Homepage_blur.jpg',
2: 'Login_bg.jpg',
3: 'home_huddle.jpg',
},
swiperOption: {
notNextTick: true,
loop: true,
initialSlide: 0,
autoplay: {
delay: 1500,
disableOnInteraction: true
},
speed: 800,
grabCursor: true,
pagination: {
el: ".swiper-pagination",
clickable: true,
type: "bullets"
}
}
}
},
}
</script>
推荐阅读
- python - Python .NET 中的 VB.NET ReDim 数组
- amazon-web-services - 找不到 /bin/sh Docker
- php - 未命名的数组中的Wordpress和PHP循环?
- postgresql - 获取每个组的最大字段的整个记录
- node.js - node.js - 主页不加载,但其他页面加载
- python - Python pyautogui单击弹出按钮不起作用?
- javascript - 当存储在变量中时,Socket.io 发射到 socket.id 不起作用
- html - 如何通过双击从禁用状态启用选择标签?
- apache-kafka - kafka 的问题 - 结果“退出代码”失败,状态 = 1/FAILURE
- concrete5 - php7 xampp 上的 Concrete5 数据库导入问题