在vue項目中使用Swiper插件詳解
vue項目使用Swiper插件
場景:平時在做項目的時候,肯定會遇到圖片輪播的效果,在此,記錄使用Swiper插件的圖片輪播效果,這里只是我自己使用的一種方式
先看效果圖

第一步:下載Swiper插件
npm install swiper vue-awesome-swiper --save
第二步:在對應的組件頁面中導入該插件
在main.js中導入
import "swiper/swiper-bundle.css" import VueAwesomeSwiper from 'vue-awesome-swiper' Vue.use(VueAwesomeSwiper)
在對應的組件頁面中導入
import Swiper from 'swiper/swiper-bundle.esm.js';
var swiper = new Swiper('.swiper');第三步: 編寫對應的html部分
<div class="swiper">
<swiper ref="mySwiper" :options="swiperOptions">
<swiper-slide v-for="(item,i) in imgList_2" :key="i">
<img :src="item.url" :alt="item.name">
</swiper-slide>
</swiper>
<div class="swiper-pagination" slot="pagination"></div>
<!-- 如果需要導航按鈕 -->
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>:options="swiperOptions" 對應的配置
第四步:在data中配置對應的參數
data() {
return {
swiperOptions: {
// loop: true, // 循環(huán)模式選項
pagination: {// 如果需要分頁器
el: '.swiper-pagination'
},
autoplay: {
delay: 5000,//自動滾動的間隔時間
stopOnLastSlide: false,//如果設置為true,當切換到最后一個slide時停止自動切換(loop模式下無效)。
disableOnInteraction: false,//用戶操作swiper之后,是否禁止autoplay。默認為true
},
// 如果需要前進后退按鈕
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
}
};
}到這一步就基本完成了一個簡單的圖片切換效果,如果想要其它的效果可以參考官網的API
vue使用swiper插件時遇到的錯誤
今天在用vue使用swiper插件時遇到這樣的錯誤
vue-awesome-swiper.js?7212:1 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘_c’)

附上導入的代碼:
import { swiper, swiperSlide} from 'vue-awesome-swiper'
import 'swiper/swiper-bundle.css'
解決方案
在vue-awesome-swiper后面添加/src

import { swiper, swiperSlide} from 'vue-awesome-swiper/src'
import 'swiper/swiper-bundle.css'
附上完整代碼:
<template>
<div>
<swiper :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>
</div>
</template>
<script>
// swiper插件
import { swiper, swiperSlide} from 'vue-awesome-swiper/src'
import 'swiper/swiper-bundle.css'
export default {
name: "TestComponent",
// 注冊組件
components: {
swiper,
swiperSlide
},
data() {
return {
swiperOptions: {
slidesPerView: 4,//顯示個數
direction: 'horizontal', // 水平方向
pagination: {
el: '.swiper-pagination'
},
}
}
},
}
</script>
<style scoped>
</style>
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
使用Element的InfiniteScroll 無限滾動組件報錯的解決
這篇文章主要介紹了使用Element的InfiniteScroll 無限滾動組件報錯的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07

