# 前言

# ✨魁首

🔔一个没有天赋的前端程序员👨‍💻 💡博客

# 🔰关注我

🤖Github |🔑CSDN | 💎掘金 | 📖 知乎 | 💡博客 |

# 一、弹窗中的 swiper 再次打开后,分页器不见了?

我的插件版本

"swiper": "5.4.5",
"vue-awesome-swiper": "^4.1.1",
1
2

# 问题描述:

第一次打开分页器的时候能正常看到分页器,第二次打开分页器神奇般的不见了,最开始我判断的方向有两个, ①分页器初始化的时候,swiper 在 imgList 还没获取到就提前加载了; ②关闭分页器的时候,swiper 未能完全销毁,重新切换一个tab页或者刷新网页又能出现; 在查阅了很多资料之后,发现以上两个判断都咩有准确的解决方案,干脆一不做二不休,自定义一个分页器;

# 解决方案:自定义分页器

<!-- mySwiper 自定义弹窗 -->
<template>
	<a-modal class="payedOrderModal" :keyboard="false" :maskClosable="true" :footer="null" :destroyOnClose="!0" centered :body-style="{paddingTop:0,paddingBottom:0,height:'500px',minHeight:'500px'}" :width="990" :visible="showFlag" cancelText="取消" @cancel="cancel">
		<div slot="title">图片</div>
		<swiper class="swiper mySwiper" ref="mySwiper" :autoplay="true" :options="swiperOptions">
			<swiper-slide class="swiper-slide" v-for="(item,index) of imgList" :key="index">
				<div class="img swiper-zoom-container">
					<img :src="item.url || item" alt="">
				</div>
			</swiper-slide>
			<!-- <div class="swiper-pagination" slot="pagination"></div> -->
			<div class="swiper-button-prev" slot="button-prev" @click="prev"></div><!-- 左箭头 -->
			<div class="swiper-button-next" slot="button-next" @click="next"></div><!-- 右箭头 -->
		</swiper>
        <!-- 自定义分页器 -->
		<div class="swiper-pagination">
			<template v-for="(item, index) in imgList" >
				<span :class="['swiper-pagination-bullet', currentIndex === index ? bulletActive : '']" :key="index" tabindex="0" role="button" :aria-label="`Go to slide ${index}`" @click="selectPagination(index)"></span>
			</template>
		</div>
	</a-modal>
</template>

<script>
import "swiper/css/swiper.css";
import { Swiper, SwiperSlide } from "vue-awesome-swiper";
export default {
	name: "MySwiper",
	title: "这里是页面或组件名字",
	components: {
		Swiper,
		SwiperSlide
	},
	props:{
        // 当前图片索引
		activeIndex:{
			type:Number,
			default(){
				return 0;
			}
		},
        // 所有图片列表
		imgList:{
			type:Array,
			default(){
				return [];
			}
		}
	},
	data() {
		return {
			showFlag: true,
			currentIndex: null,
			bulletActive: "swiper-pagination-bullet-active",
			swiperOptions: {
				initialSlide:0,
				loop: true,
				autoplay: false,
				zoom: {
					minRatio:0.8,
					maxRatio:3
				},
				navigation: {
					nextEl: ".swiper-button-next",
					prevEl: ".swiper-button-prev"
				},
				observer: true,
				observeParents: true
			}
		};
	},
	created() {
	},
	mounted() {
		this.swiperOptions.initialSlide = this.activeIndex;
		this.currentIndex = this.activeIndex;
	},
	methods: {
		onSwiper(swiper) {
			console.log(swiper);
		},
		onSlideChange() {
			console.log("slide change");
		},
		prev(){
			let { realIndex } = this.$refs.mySwiper.$swiper;
			realIndex = !realIndex ? this.imgList?.length : this.currentIndex;
			this.$set(this, "currentIndex", realIndex - 1);
			this.$refs.mySwiper.$swiper.slidePrev();
		},
		next(){
			let { realIndex } = this.$refs.mySwiper.$swiper;
			realIndex = realIndex === this.imgList?.length - 1 ? -1 : this.currentIndex;
			this.$set(this, "currentIndex", realIndex + 1);
			this.$refs.mySwiper.$swiper.slideNext();
		},
		selectPagination(index){
			this.currentIndex = index;
			this.$refs.mySwiper.$swiper.slideTo(index + 1);
		},
		cancel(){
			this.showFlag = false;
			this.$emit("cancel");
		}
	},
	beforeDestroy() {
		this.$refs.mySwiper.$swiper.destroy();
	}
};
</script>

<style scoped lang="less">
.swiper-slide{
	text-align: center;
	padding: 10px;
	img{
		max-width: 100%;
		max-height: 60vh;
	}
}
.mySwiper{
    height: 100%;
    /deep/.swiper-wrapper{
        height: 100%;
        display: flex;
        align-items: center;
    }
}
.swiper-pagination{
	bottom: 10px;
    left: 0;
    width: 100%;
	.swiper-pagination-bullet{
		margin: 0 4px;
	}
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137