无法加载

vue-awesome-swiper的使用方法

FavoriteLoading收藏

现在网上对于vue-awesome-swiper的使用方法各种坑,有很多版本对不上、swiper.css引用地址不对、swiper-pagination不显示、要么自动轮播失效各种问题出现。下面介绍一下自己亲测可用的正确使用方法。

这里我使用的3.1版本,直接这样安装就行。

npm install vue-awesome-swiper@3 --save-dev

简单基本配置,贴一下我的代码

<template>
  <div class="recommendPage">
    <swiper :options="swiperOption" ref="mySwiper">
      <swiper-slide>I'm Slide 1</swiper-slide>
      <swiper-slide>I'm Slide 2</swiper-slide>
      <swiper-slide>I'm Slide 3</swiper-slide>
      <div class="swiper-pagination" slot="pagination"></div>
      <div class="swiper-button-prev" slot="button-prev"></div>
      <div class="swiper-button-next" slot="button-next"></div>
    </swiper>
  </div>
</template>


<script>
// 引入插件
import { swiper, swiperSlide } from "vue-awesome-swiper";
import "swiper/dist/css/swiper.css";


export default {
  components: {
    swiper,
    swiperSlide
  },
  data() {
    return {
      swiperOption: {
        loop: true,
        autoplay: {
          delay: 3000,
          stopOnLastSlide: false,
          disableOnInteraction: false
        },
        // 显示分页
        pagination: {
          el: ".swiper-pagination",
          clickable: true //允许分页点击跳转
        },
        // 设置点击箭头
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev"
        }
      }
    };
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.swiper;
    }
  },
};
</script>
<style lang="scss" scoped >
.recommendPage .swiper-container{
  position: relative;
  width: 100%;
  height: 200px;
  background: pink;
}  
.recommendPage .swiper-container .swiper-slide{
  width: 100%;
  line-height: 200px;
  background: yellowgreen;
  color: #000;
  font-size: 16px;
  text-align: center;
}
</style>

上面是在页面引入,如果有小伙伴全局引入也是可以的,直接在main.js引入

import Vue from 'vue'
import VueAwesomeSwiper from 'vue-awesome-swiper'
 
//引入样式
import 'swiper/css/swiper.css'
 
Vue.use(VueAwesomeSwiper, /* { 全局组件的默认选项 } */)

对于一些Options的设置可以查看一下github的vue-awesome-swiper的官方示例:
https://github.surmon.me/vue-awesome-swiper/

留下评论

微信:15182814906

QQ:1548902957

邮箱:1548902957@qq.com