vue-react-swiper6-组件封装-解决坑点loop点击和滑动失效等问题-完整代码

话不多说直接上代码
安装依赖:

npm install swiper

组件代码:

<template>
    <div class="swiper-box">
        <div class="swiper-container">
            <div class="swiper-wrapper">
                <slot name="img"></slot>
            </div>
            <div class="swiper-pagination"></div>
        </div>
    </div>
</template>
<script>
import Swiper from "swiper/swiper-bundle.min.js";
import "swiper/swiper-bundle.min.css";

// 当前版本"swiper": "^6.4.8"

// template部分
// <Banner :key="list.length" :clickBanner="clickBanner"> // 1、v-bind:key="list.length"防止渲染后loop失效
//     <img slot="img" v-for="(item, index) in list" :key="index" class="swiper-slide" :src="item.src" :alt="item.name">
// </Banner>

// script部分代码
// import Banner from './components/banner'
// components: {
//     Banner
// },
// created() {
//      getBanners().then(res => {
//          this.list = res.data.list  // [{src: './assets/img/banner01.jpg', id: '001', name: '图1'}]
//      })
// }

export default {
    props: {
        clickBanner: Function, // 点击获取当前索引
        options: {
            type: Object,
            default: () => {
                return {
                    loop: true,
                    observer:true,//修改swiper自己或子元素时,自动初始化swiper
                    observeParents:true,//修改swiper的父元素时,自动初始化swiper
                    pagination: {
                        el: ".swiper-pagination"
                    }
                }
            }
        }
    },
    data() {
        return {}
    },
    mounted() {
        const VM = this
        const myOption = {
            ...this.options,
            on: {
                click: function () {
                    // 这里有坑,需要注意的是:this 指向的是 swpier 实例,而不是当前的 vue, 因此借助 vm,来调用 methods 里的方法
                    // console.log(this); // this === Swiper
                    // 当前活动块的索引,与activeIndex不同的是,在loop模式下不会将 复制的块 的数量计算在内。
                    VM.clickBanner(this.realIndex);
                }
            }
        }
        new Swiper(".swiper-container", myOption)
    }
};
</script>
<style scoped>
.swiper-box{
    width: 100%;
}
.box >>> .swiper-container{
    width: 100%;
}
</style>

如何使用该组件:

<template>
  <div id="app">
      <Banner :key="list.length" :clickBanner="clickBanner">
        <img slot="img" v-for="(item, index) in list" :key="index" class="swiper-slide" :src="item.src" :alt="item.name">
      </Banner>
  </div>
</template>

<script>
import Banner from './components/banner'
export default {
  name: 'App',
  components: {
    Banner
  },
  data () {
      return {
        list: []
      }
  },
  created() {
    setTimeout(() => { // 模拟api请求
      this.list = [
        {src: require('./assets/img/banner01.jpg'), id: '001', name: '图1'},
        {src: require('./assets/img/banner02.jpg'), id: '002', name: '图2'},
        {src: require('./assets/img/banner03.jpg'), id: '003', name: '图3'}
      ]
    }, 500)
  },
  methods: {
    clickBanner(index) {
      console.log(this.list[index])
    }
  }
}
</script>

<style scoped>
html,body,#app{
  width: 100%;
  height: 100%;
}
.swiper-slide{
  display: block;
  width: 100%;
  height: 200px;
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值