【问题标题】:How can I make vue-awesome-swiper loop normally and display the pagination?如何正常制作 vue-awesome-swiper 循环并显示分页?
【发布时间】: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个奇怪的问题:

  1. 我发现图像无法正常循环,即使我在脚本中将循环设置为 true,当到达最后一张图像时它总是停止。
  2. 分页无法显示

所以我去github问题寻求帮助。遇到同样问题的人通过在swiper元素中使用v-if="banners.length!=0"来解决这些问题。但是我很困惑为什么这两个问题与横幅的长度有关?有人可以解释一下这个?

【问题讨论】:

    标签: vue.js swiper


    【解决方案1】:

    您的视图中似乎缺少&lt;v-content&gt; 标签。

    您的整个视图应该用&lt;v-content&gt; 包裹,以确保大小正确。您可以在此处查看文档: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>
    

    【讨论】:

    • 这和Vuetify无关,只是vue
    • 我用代码和框重新创建了您的滑块,它可以 100% 工作。看看这里:codesandbox.io/s/spring-tdd-nxcdf?file=/swiperView.vue 我建议用这个例子仔细检查你的 vue-awesome-swiper 依赖。我尝试使用您的 CSS 导入,但沙箱对此不满意,因此进行了更改。也许这就是导致您的分页消失的原因。希望这会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多