在組件中使用輪播圖展示圖片信息:
1.下載swiper,5版本為穩(wěn)定版本
cnpm install swiper@5
2.在組件中引入swiper包和對應(yīng)樣式,若多組件使用swiper,可以把swiper引入到main.js入口文件中:
import 'swiper/css/swiper.css' //引入swiper樣式
import Swiper from 'swiper'; //引入swiper
?3.在組件中使用:
由于我的組件獲取的圖片信息由axios請求后端獲得,所以要借助屬性監(jiān)聽+$nextTick()共同實現(xiàn)輪播圖:
<template>
<div class="list-container">
<div class="sortList clearfix">
<div class="center">
<!--banner輪播-->
<div class="swiper-container" ref="mySwiper">
<div class="swiper-wrapper">
<div
class="swiper-slide"
v-for="carousel in bannerList"
:key="carousel.id"
>
<img :src="carousel.imgUrl" />
</div>
</div>
<!-- 如果需要分頁器 -->
<div class="swiper-pagination"></div>
<!-- 如果需要導(dǎo)航按鈕 -->
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import {mapState} from 'vuex'
import Swiper from 'swiper';
export default {
name: 'ListContainer',
mounted(){
//在vuex倉庫中把輪播圖和banner圖片取出來
this.$store.dispatch('BannerList')
},
computed:{
...mapState({
bannerList:(state)=>state.home.bannerList
})
},
watch:{
bannerList:{
handler(newValue,oldValue){
this.$nextTick(()=>{
var mySwiper=new Swiper(this.$refs.mySwiper,{
//配置分頁器內(nèi)容
loop: true, // 循環(huán)模式選項
pagination:{
el:".swiper-pagination",//換頁器與哪個標簽關(guān)聯(lián)
clickable:true//分頁器是否可以點擊
},
// 如果需要前進后退按鈕
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
//如果需要滾動條
scrollbar: {
el: '.swiper-scrollbar',
},
})
})
}
}
}
}
</script>
<style lang="less" scoped>
</style>
文章來源:http://www.zghlxwxcb.cn/news/detail-649263.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-649263.html
到了這里,關(guān)于在vue中使用swiper輪播圖(搭配watch和$nextTick())的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!