Vue使用swiper问题怎么解决

其他教程   发布日期:2025年02月13日   浏览次数:639

本文小编为大家详细介绍“Vue使用swiper问题怎么解决”,内容详细,步骤清晰,细节处理妥当,希望这篇“Vue使用swiper问题怎么解决”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

一、下载指定版本swiper

  1. npm i swiper@5.2.0

二、创建轮播图组件CarouselContainer.vue

详细解析在代码注释中

  1. <template>
  2. <div
  3. class="CarouselContainer"
  4. @mouseenter="stopAutoPlay"
  5. @mouseleave="startAutoPlay"
  6. >
  7. <div ref="mySwiper" class="swiper-container" :id="currentIndex">
  8. <div class="swiper-wrapper">
  9. <div
  10. class="swiper-slide my-swiper-slide"
  11. v-for="(item, index) of slideList"
  12. :key="index"
  13. >
  14. {{ item }}
  15. </div>
  16. </div>
  17. <!-- 分页器 -->
  18. <!-- <div class="swiper-pagination"></div> -->
  19. <!--导航器-->
  20. <div class="swiper-button-prev"></div>
  21. <div class="swiper-button-next"></div>
  22. </div>
  23. </div>
  24. </template>
  25. <script>
  26. import Swiper from "swiper";
  27. import "swiper/css/swiper.css";
  28. export default {
  29. name: "CarouselContainer",
  30. props: ["slideList", "currentIndex"],
  31. data() {
  32. return {
  33. currentSwiper: null,
  34. };
  35. },
  36. watch: {
  37. //slide数据发生变化时,更新swiper
  38. slideList: {
  39. deep: true,
  40. // eslint-disable-next-line
  41. handler(nv, ov) {
  42. console.log("数据更新了");
  43. this.updateSwiper();
  44. },
  45. },
  46. },
  47. mounted() {
  48. this.initSwiper();
  49. },
  50. methods: {
  51. //鼠标移入暂停自动播放
  52. stopAutoPlay() {
  53. this.currentSwiper.autoplay.stop();
  54. },
  55. //鼠标移出开始自动播放
  56. startAutoPlay() {
  57. this.currentSwiper.autoplay.start();
  58. },
  59. //初始化swiper
  60. initSwiper() {
  61. // eslint-disable-next-line
  62. let vueComponent = this; //获取vue组件实例
  63. //一个页面有多个swiper实例时,为了不互相影响,绑定容器用不同值或变量绑定
  64. this.currentSwiper = new Swiper("#" + this.currentIndex, {
  65. // 循环模式选项loop,默认为false,可动态设置,当slide只有一页时置为false,>1页时置为true
  66. loop: true, // 循环模式选项
  67. autoHeight: "true", //开启自适应高度,容器高度由slide高度决定
  68. //分页器
  69. // pagination: {
  70. // el: '.swiper-pagination',
  71. // clickable:true,//分页器按钮可点击
  72. // },
  73. //grabCursor: true, //小手掌抓取滑动
  74. // direction: "vertical", // 纵向滚动,默认是横向滚动的
  75. on: {
  76. //此处this为swiper实例
  77. //切换结束获取slide真实下标
  78. slideChangeTransitionEnd: function () {
  79. console.log(vueComponent.$props.currentIndex+"号swiper实例真实下标",this.realIndex)
  80. },
  81. //绑定点击事件,解决loop:true时事件丢失
  82. // eslint-disable-next-line
  83. click: function (event) {
  84. console.log("你点击了"+vueComponent.$props.currentIndex+"号swiper组件")
  85. },
  86. },
  87. //导航器
  88. navigation: {
  89. nextEl: ".swiper-button-next",
  90. prevEl: ".swiper-button-prev",
  91. },
  92. autoplay: {
  93. //自动播放,不同版本配置方式不同
  94. delay: 2000,
  95. stopOnLastSlide: false,
  96. disableOnInteraction: false, // 用户操作之后是否停止自动轮播默认true
  97. },
  98. slidesPerView: 1, //视口展示slide数1
  99. slidesPerGroup: 1, //slide数1页一组
  100. });
  101. },
  102. //销毁swiper
  103. destroySwiper() {
  104. try {
  105. // 此处destroy(bool1,bool2)
  106. // bool1代表是否销毁swiper实例
  107. // bool2代表是否销毁swiper样式(导航器、分页器等)
  108. this.currentSwiper.destroy(true, false);
  109. } catch (e) {
  110. console.log("删除轮播");
  111. }
  112. },
  113. //更新swiper(先销毁 再 重新初始化swiper)
  114. updateSwiper() {
  115. this.destroySwiper();
  116. this.$nextTick(() => {
  117. this.initSwiper();
  118. });
  119. },
  120. },
  121. // 组件销毁之前 销毁 swiper 实例
  122. beforeDestroy() {
  123. this.destroySwiper();
  124. },
  125. };
  126. </script>
  127. <style scoped lang="scss">
  128. .CarouselContainer {
  129. width: 100%;
  130. height: 100%;
  131. background-color: gray;
  132. }
  133. /*slide样式*/
  134. .my-swiper-slide {
  135. height: 300px;
  136. background-color: pink;
  137. }
  138. /*swiper容器样式*/
  139. .swiper-container {
  140. width: 700px;
  141. border: 1px solid red;
  142. }
  143. // /*自定义分页器按钮被点击选中时的样式*/
  144. // ::v-deep(.swiper-pagination-bullet-active){
  145. // background-color: #d5a72f !important;
  146. // width: 20px;
  147. // }
  148. // /*自定义分页器按钮常规样式*/
  149. // ::v-deep(.swiper-pagination-bullet){
  150. // background-color: #9624bf;
  151. // opacity: 1;
  152. // width: 20px;
  153. // }
  154. </style>

三、创建父组件Father.vue渲染多个swiper组件、模拟异步数据变化

  1. <template>
  2. <div class="father">
  3. <!--传递不同的currentIndex 作为区分不同swiper组件的动态id-->
  4. <CarouselContainer :slide-list="list" currentIndex="1"></CarouselContainer>
  5. <CarouselContainer :slide-list="list" currentIndex="2"></CarouselContainer>
  6. <button @click="changeData">更换数据喽</button>
  7. </div>
  8. </template>
  9. <script>
  10. import CarouselContainer from './components/CarouselContainer.vue'
  11. export default {
  12. components: {
  13. CarouselContainer,
  14. },
  15. data(){
  16. return{
  17. list:['a','b','c']
  18. }
  19. },
  20. methods: {
  21. changeData(){
  22. const swiperList = ['我是图片1','我是图片2','我是图片3'];
  23. this.list = swiperList ;
  24. }
  25. }
  26. }
  27. </script>
  28. <style scoped>
  29. </style>

完成之后就可以在你的项目中看到效果啦,之后可以根据项目需求去改进&hellip;

修改

此处追加说明destroy()释放swiper实例的几种情况:

解决问题:内存增长

此处destroy(bool1,bool2)

  • bool1代表是否销毁swiper实例

  • bool2代表是否销毁swiper样式(导航器、分页器等)

情景一:如果只更新swiper里面的数据,destroy(true,false)

情景二:如果要销毁(跳转路由销毁组件,遍历重新new一个swiper实例)swiper实例,destroy(true,true)

以上就是Vue使用swiper问题怎么解决的详细内容,更多关于Vue使用swiper问题怎么解决的资料请关注九品源码其它相关文章!