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

看官方文檔更詳細

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM