vue中使用swiper輪播圖的正確姿勢(shì)(親測(cè)有效)
前言
網(wǎng)上搜了一大堆在vue中如何使用swiper,結(jié)果搜出來(lái)一堆垃圾,也不知道從哪里復(fù)制的,吐槽完畢。假設(shè)你是個(gè)新手,我從新建項(xiàng)目開始跟你講,以下是步驟。
1.新建vue項(xiàng)目
vue create 項(xiàng)目名
然后選最下面那一個(gè)(鍵盤上下鍵操作)然后回車

選擇Bable,Router,Vuex,Css-Processords四個(gè),其他的不要選中(空格鍵是選中和取消選中)

剩下的步驟按這張圖來(lái)進(jìn)行選擇,然后項(xiàng)目就創(chuàng)建成功了

2.裝swiper的包
先在命令行cd到項(xiàng)目中
cd 項(xiàng)目名
npm i swiper vue-awesome-swiper
npm i swiper@5
在package.json中查看裝包是否完成

3.使用swiper
1.在components文件夾中新建swiperCom.vue,把下面代碼復(fù)制進(jìn)去,注釋里面有swiper的使用方法。
注意:請(qǐng)確保../assets/img/ 路徑下有swiper1.jpg等照片
<template>
<div id="swipercom">
<div class="swiper-container" id="swiperIndex">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="(item,i) in imgs" :key="i">
<img :src="item.pic" alt="">
</div>
</div>
//換頁(yè)器
<div class="swiper-pagination">
</div>
//前進(jìn)后退
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
//滾動(dòng)條
<div class="swiper-scrollbar"></div>
</div>
</div>
</template>
<script>
import 'swiper/css/swiper.css' //引入swiper樣式
import Swiper from 'swiper'; //引入swiper
export default {
name: "Swiper",
data(){
return{
imgs:[
{pic:require('../assets/img/swiper1.jpg')},
{pic:require('../assets/img/swiper2.jpg')},
{pic:require('../assets/img/swiper3.png')}
]
}
},
mounted() {
var mySwiper=new Swiper('#swiperIndex',{
//配置分頁(yè)器內(nèi)容
loop: true, // 循環(huán)模式選項(xiàng)
pagination:{
el:".swiper-pagination",//換頁(yè)器與哪個(gè)標(biāo)簽關(guān)聯(lián)
clickable:true//分頁(yè)器是否可以點(diǎn)擊
},
// 如果需要前進(jìn)后退按鈕
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
//如果需要滾動(dòng)條
scrollbar: {
el: '.swiper-scrollbar',
},
})
}
}
</script>
<style lang="less">
#swipercom{
width: 7.5rem;
#swiperIndex.swiper-container{
width: 7.1rem;
height: 2.6rem;
border-radius: 0.1rem;
.swiper-slide img{
width: 100%;
}
.swiper-pagination-bullet-active{
background-color: orangered;
}
}
}
</style>
2.然后在項(xiàng)目中找到HomeView.vue(默認(rèn)為主頁(yè)面),把下面代碼復(fù)制,替換掉里面內(nèi)容,里面引入了swiperCom子組件,這也是我們需要用到swiper的子組件
<template>
<div class="home">
<!-- 輪播圖-->
<swiperCom></swiperCom>
</div>
</template>
<script>
import SwiperCom from "@/components/swiperCom";
export default {
name: 'HomeView',
components: {
SwiperCom
}
}
</script>
<style scoped>
</style>
大功告成,效果如下

除此之外要是想要有更多的輪播圖樣式可以去swiper官網(wǎng)進(jìn)行查閱
https://www.swiper.com.cn/usage/index.html
總結(jié)
到此這篇關(guān)于vue中使用swiper輪播圖的正確姿勢(shì)的文章就介紹到這了,更多相關(guān)vue使用swiper輪播圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
運(yùn)行npm?run?dev報(bào)錯(cuò)的原因及解決
剛剛創(chuàng)建好vue項(xiàng)目的時(shí)候,運(yùn)行 npm run dev 會(huì)報(bào)錯(cuò),下面這篇文章主要給大家介紹了關(guān)于運(yùn)行npm?run?dev報(bào)錯(cuò)的原因及解決方法,需要的朋友可以參考下2022-10-10
Vue中代碼傳送(teleport)的實(shí)現(xiàn)
本文主要介紹了Vue中代碼傳送(teleport)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
詳解Vue的Pinia如何做到刷新不丟數(shù)據(jù)
Pinia 是 Vue 3 的官方推薦狀態(tài)管理庫(kù),旨在替代 Vuex,提供更簡(jiǎn)單、直觀的狀態(tài)管理解決方案,Pinia 的設(shè)計(jì)理念是簡(jiǎn)單、易于學(xué)習(xí)和使用,本文給大家詳細(xì)介紹了Vue的Pinia如何做到刷新不丟數(shù)據(jù),需要的朋友可以參考下2025-01-01
VSCode創(chuàng)建Vue項(xiàng)目的完整步驟教程
Vue是一個(gè)輕量級(jí)、漸進(jìn)式的Javascript框架,如果想要要開發(fā)全新的Vue項(xiàng)目,建議項(xiàng)目構(gòu)建工具vue-cli,下面這篇文章主要給大家介紹了關(guān)于VSCode創(chuàng)建Vue項(xiàng)目的完整步驟,需要的朋友可以參考下2022-06-06
利用nginx部署vue項(xiàng)目的全過(guò)程
今天要用到服務(wù)器nginx,還需要把自己的vue的項(xiàng)目部署到服務(wù)器上去所以就寫一下記錄下來(lái),下面這篇文章主要給大家介紹了關(guān)于利用nginx部署vue項(xiàng)目的相關(guān)資料,需要的朋友可以參考下2022-03-03
vue 中Virtual Dom被創(chuàng)建的方法
本文將通過(guò)解讀render函數(shù)的源碼,來(lái)分析vue中的vNode是如何創(chuàng)建的,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
Vue ElementUI實(shí)現(xiàn):限制輸入框只能輸入正整數(shù)的問(wèn)題
這篇文章主要介紹了Vue ElementUI實(shí)現(xiàn):限制輸入框只能輸入正整數(shù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
vue3響應(yīng)式Object代理對(duì)象的讀取示例詳解
這篇文章主要為大家介紹了vue3響應(yīng)式Object代理對(duì)象的讀取示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
解決antd日期選擇組件,添加value就無(wú)法點(diǎn)擊下一年和下一月問(wèn)題
這篇文章主要介紹了解決antd日期選擇組件,添加value就無(wú)法點(diǎn)擊下一年和下一月問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10

