LoginSignup
0
0

More than 3 years have passed since last update.

vue-awesome-swiperのインストール

Posted at

この記事について

swiperのバージョンによるcssの読み込み先の違いやOptionについて詰まったため記録

配布元:vue-awesome-swiper
DEMO:https://github.surmon.me/vue-awesome-swiper/

開発環境

  • Mac OS
  • Node.js v12.13.1
  • npm 6.12.1
  • Vue CLI 3.9.0
  • vue 2.6.2
  • Mac OS

実装方法 (グローバルに読み込み)

配布元のインストール方法に則って実施。

ターミナル
npm install swiper vue-awesome-swiper --save

先ほどのパッケージインストールでpackage.jsonにswiperが追加されているためバージョンを確認する。

package.json
  "dependencies": {
    "core-js": "^3.6.5",
    "reset.css": "^2.0.2",
    "swiper": "^6.5.3",
    "vue": "^2.6.11",
    "vue-awesome-swiper": "^4.1.1",
    "vue-router": "^3.2.0",
    "vue-slide-up-down": "^2.0.1"
  },

main.jsにインポートする
※ swiperのバージョンに合ったCSSを読み込む

main.js
import VueAwesomeSwiper from 'vue-awesome-swiper'
// import style (>= Swiper 6.x)
import 'swiper/swiper-bundle.css'
// import style (<= Swiper 5.x)
import 'swiper/css/swiper.css'
Vue.use(VueAwesomeSwiper, /* { default options with global component } */)

コンポーネント化

Carousel.vue
<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: 'Carousel',
    data() {
      return {
        swiperOptions: {
          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> 

使用する箇所にインポート

Main.vue
<template>
  <div class="Main">
    <Carousel></Carousel>
  </div>
</template>

<script>
import Carousel from '@/components/Carousel.vue'

export default {
  name: 'Main',
  components: {
    Carousel,
  }
}
</script>

おまけ

Optionについて

使ったOptionについて備忘
* loopオプションがtrueの時、「loopAdditionalSlidesオプションを指定しないと一番最後のスライドに行った時に逆回りで最初に戻ってしまう。

vue
<script>
  export default {
    data() {
      return {
        swiperOptions: {
        loop: true, //無限ループ
        slidesPerView: 3, //画面に何枚表示させるか
        spaceBetween: 10, //スライド間隔
        speed: 1000,
        autoplay: {
          delay: 2000,
          disableOnInteraction: true//何らかのアクション後の自動切り替えを再開
        },
        breakpoints: { //ブレイクポイント
          760: {
            slidesPerView: 1,
            width:250, //横幅をpx指定も可能
            loopAdditionalSlides: 4,//Addition number of slides that will be cloned after creating of loop
            spaceBetween: 30,
            speed: 1500,
            grabCursor: true, //マウスで次のスライドに行くことができる
            autoplay: {
              delay: 1000,
              stopOnLastSlide: false,
              disableOnInteraction: false//何らかのアクション後の自動切り替えを再開
            }
          },
        }
      }
    }
  },
  }
</script>

参考サイト:
Swiper API
Nuxt.jsでswiper(vue-awesome-swiper)を入れてみた
【Vue.js】Vue.js(Nuxt.js)でスライダーを導入してみた【vue-awesome-swiper】

発生したエラーについて

main.jsにswiper.cssの読み込みを記載した際にCSSの読み込みエラーが発生

ターミナルのエラー
* swiper/css/swiper.css in ./src/main.js

原因:インストールされたswiperのバージョンの違いで、cssの読み込み先が違った

main.js
// import style (>= Swiper 6.x)
import 'swiper/swiper-bundle.css'
// import style (<= Swiper 5.x)
import 'swiper/css/swiper.css'

参考サイト:
https://github.com/surmon-china/vue-awesome-swiper/issues/693

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0