vue 使用swiper

swiper在vue的严格模式下我最终放弃来。我下载的是swiper5没改好,用了swiper给vue定制的  vue-awesome-swiper

 

 

默认需要先安装

npm install swiper vue-awesome-swiper --save

# or
yarn add swiper vue-awesome-swiper

 

1.全局使用方法(在main)

 

import Vue from 'vue'
import VueAwesomeSwiper from 'vue-awesome-swiper'

// import style
import 'swiper/css/swiper.css'

Vue.use(VueAwesomeSwiper, /* { default options with global component } */)

 

2.在使用的组件中使用

<template>
  <swiper ref="mySwiper" :options="swiperOptions">
    <swiper-slide>Slide 1</swiper-slide>
    <swiper-slide>Slide 2</swiper-slide>
    <swiper-slide>Slide 3</swiper-slide>
    <swiper-slide>Slide 4</swiper-slide>
    <swiper-slide>Slide 5</swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
</template>

<script>
  export default {
    name: 'carrousel',
    data() {
      return {
        swiperOptions: {
slidesPerView: 3, // 显示的数量
spaceBetween: 30, // 每张的间距
pagination: { el: '.swiper-pagination' }, // Some Swiper option/callback...  } } }, computed: { swiper() { return this.$refs.mySwiper.$swiper } }, mounted() { console.log('Current Swiper instance object', this.swiper) this.swiper.slideTo(3, 1000, false) } } </script>

 

这样就可以了

 

 

如果是局部使用的话,只需要在使用的组件中引入,在组件使用应该是一样的

import { Swiper, SwiperSlide, directive } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'

export default {
  components: {
    Swiper,
    SwiperSlide
  },
  directives: {
    swiper: directive
  }
}

 

附上官方地址:https://github.com/surmon-china/vue-awesome-swiper

看官方文档更详细

 

posted @ 2020-03-28 10:54  lude1994  阅读(8262)  评论(1编辑  收藏  举报