vue+swiper實現(xiàn)組件化開發(fā)的實例代碼
swiper的組件
<template>
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" v-for="item in swiper"><img :src="item.room_src" alt=""></div>
<!--<div class="swiper-slide" v-for="item in test"><img :src="item.room_src" alt=""></div>-->
</div>
</div>
</template>
<script>
import Swiper from 'swiper'
export default {
name: 'swiper',
data() {
return {
mySwiper: null,
// test: [
// "https://rpic.douyucdn.cn/acrpic/171024/288016_0921.jpg",
// "https://rpic.douyucdn.cn/acrpic/171024/748396_0924.jpg",
// "https://rpic.douyucdn.cn/acrpic/171024/453751_0922.jpg",
// "https://rpic.douyucdn.cn/acrpic/171024/79663_0920.jpg"
// ]
}
},
props: ['swiper'], //swiper的就是test這個數(shù)據(jù)傳遞來的
methods: {
_initSwiper() {
this.mySwiper = new Swiper('.swiper-container', {
autoplay: 5000,//可選選項,自動滑動
})
},
_updateSwiper() {
this.$nextTick(() => {
this.mySwiper.update(true); //swiper update的方法
})
},
swiperUpdate() {
if (this.mySwiper) { //節(jié)點存在
this._updateSwiper(); //更新
} else {
this._initSwiper(); //創(chuàng)建
}
},
},
watch: {
//通過props傳來的數(shù)據(jù) 和 組件一加載節(jié)點就創(chuàng)建成功 二者不是同步,實時監(jiān)聽的swiper(傳遞的值)的變化
swiper() {
this.swiperUpdate();
}
},
mounted() {
this.swiperUpdate(); //頁面一加載拉去數(shù)據(jù)創(chuàng)建節(jié)點
}
}
</script>
<style lang="scss" scoped>
.swiper-container {
width: 100%;
height: 4rem;
margin-top: 0.9rem;
.swiper-wrapper {
width: 100%;
height: 100%;
.swiper-slide {
background-size: cover;
width: 100%;
height: 4rem;
img {
width: 100%;
height: 100%;
}
}
}
}
</style>
home.vue 調(diào)用的組件方法
//html
<swiper :swiper="roomList.slice(6,10)" ></swiper>
//js
import swiper from 'components/swiper/swiper'
components: {
swiper
},
問題:如果單純的調(diào)用_initSwiper的方法,會發(fā)現(xiàn)頁面是不能滾動的,但是頁面隨便修改東西,然后保存的swiper又可以滾動的,這個個原因是初始swiper的節(jié)點沒有創(chuàng)建成功,值頁面有穿進去的,一層一層的可以打印swiper的值為空的,當修改東西值就能傳遞進去的,所以的這里的我們需要通過判斷節(jié)點是否成功來update siwper的方法
相關(guān)文章
Vue 級聯(lián)下拉框的設(shè)計與實現(xiàn)
在前端開發(fā)中,級聯(lián)選擇框是經(jīng)常用到的,這樣不僅可以增加用戶輸入的友好性,還能減少前后端交互的數(shù)據(jù)量。本文就介紹一下使用Vue實現(xiàn)級聯(lián)下拉框,感興趣的可以了解一下2021-07-07
基于Vue2.0+ElementUI實現(xiàn)表格翻頁功能
Element UI 是一套采用 Vue 2.0 作為基礎(chǔ)框架實現(xiàn)的組件庫,它面向企業(yè)級的后臺應(yīng)用,能夠幫助你快速地搭建網(wǎng)站,極大地減少研發(fā)的人力與時間成本。這篇文章主要介紹了Vue2.0+ElementUI實現(xiàn)表格翻頁功能,需要的朋友可以參考下2017-10-10
解決Vue項目中Emitted value instead of an 
這篇文章主要介紹了解決Vue項目中Emitted value instead of an instance of Error問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
Vue中Object.assign清空數(shù)據(jù)報錯的解決方案
這篇文章主要介紹了Vue中Object.assign清空數(shù)據(jù)報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Vue網(wǎng)頁html轉(zhuǎn)換PDF(最低兼容ie10)的思路詳解
這篇文章主要介紹了Vue網(wǎng)頁html轉(zhuǎn)換PDF(最低兼容ie10)的思路詳解,實現(xiàn)此功能需要引入兩個插件,需要的朋友可以參考下2017-08-08

