1. 程式人生 > >vue 中swiper 的引入

vue 中swiper 的引入

prev ref brush rac value tps -s tel pip

最近碰到需要一個輪播的組件,在網上找了半天,也安裝了一下午,各種報錯,現在終於弄好了,方法如下。

先安裝swiper vue 版 cnpm i vue-awesome-swiper --save

版本是用最新4.X 的。

在main.js中如下引入

import‘./../static/css/swiper.min.css‘ 這個css文件是手動下載的 , 在style 中引入樣式會出問題,一直沒解決,不知道什麽原因,希望有人能解答
import VueAwesomeSwiper from ‘vue-awesome-swiper‘ 引入js

組件

<template>
	<div>
		<div class="sWipe">
		  <swiper :options="swiperOption" ref="mySwiper" class="swipeCont">
		    <!-- slides -->
		    <swiper-slide v-for="(tag,key) in list" :key="key">
		    		<img :src="tag.img" />
		    </swiper-slide>
		    
		    <!-- Optional controls -->
		    <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>
		    <!-- 如果需要滾動條 -->
    		<div class="swiper-scrollbar"></div>
		  </swiper>
	  </div>
  </div>
</template>

<script>
	
  export default {
    name: ‘carrousel‘,
    data() {
      return {
      	list: [
	            { img: ‘https://qiniu.epipe.cn/5456575529551388672?imageslim&imageView2/1/w/750/h/360‘ },
	            { img: ‘https://qiniu.epipe.cn/5430983074181545984?imageslim&imageView2/1/w/750/h/360‘ },
	            { img: ‘https://qiniu.epipe.cn/5464226412548325376?imageslim&imageView2/1/w/750/h/360‘ }
          ],
        swiperOption: {
        	pagination: {
			      el: ‘.swiper-pagination‘,
			    },
			    navigation: {
			      nextEl: ‘.swiper-button-next‘,
			      prevEl: ‘.swiper-button-prev‘,
			    },
			    // 如果需要滾動條
			    scrollbar: {
			      el: ‘.swiper-scrollbar‘,
			    },
			    loop : true,
			    autoplay: {
				    delay: 1000,//1秒切換一次
				    disableOnInteraction: false,
				  },
		        // NotNextTick is a component‘s own property, and if notNextTick is set to true, the component will not instantiate the swiper through NextTick, which means you can get the swiper object the first time (if you need to use the get swiper object to do what Things, then this property must be true)
		        // notNextTick是一個組件自有屬性,如果notNextTick設置為true,組件則不會通過NextTick來實例化swiper,也就意味著你可以在第一時間獲取到swiper對象,假如你需要剛加載遍使用獲取swiper對象來做什麽事,那麽這個屬性一定要是true
		        notNextTick: true,
		        // swiper configs 所有的配置同swiper官方api配置
		        setWrapperSize :true,
		        paginationClickable :true,
		        // if you need use plugins in the swiper, you can config in here like this
		        // 如果自行設計了插件,那麽插件的一些配置相關參數,也應該出現在這個對象中,如下debugger
		        debugger: true,
		        // swiper callbacks
		        // swiper的各種回調函數也可以出現在這個對象中,和swiper官方一樣
		        onTransitionStart(swiper){
		          console.log(swiper)
		        },
        }
      }
    },
    // you can find current swiper instance object like this, while the notNextTick property value must be true
    // 如果你需要得到當前的swiper對象來做一些事情,你可以像下面這樣定義一個方法屬性來獲取當前的swiper對象,同時notNextTick必須為true
    computed: {
      swiper() {
        return this.$refs.mySwiper.swiper
      }
    },
    mounted() {
      // you can use current swiper instance object to do something(swiper methods)
      // 然後你就可以使用當前上下文內的swiper對象去做你想做的事了
//    console.log(‘this is current swiper instance object‘, this.swiper)
//    this.swiper.slideTo(0, 1000, false)
    }
  }
</script>

<style scoped>
		
	.swipeCont{
		height: 160px;
	}
	
</style>

這樣就可以正常使用了 ,可以寫成組件用props 來替換對應的數據。

import‘./../static/css/swiper.min.css‘ 這個css文件是手動下載的 , 在style 中直接引入樣式會出問題,一直沒解決,不知道什麽原因,希望有人能解答。

vue 中swiper 的引入