vue+swiper實現組件化開發(fā)的實例代碼
更新時間:2017年10月26日 10:43:59 作者:zhooson
這篇文章主要介紹了vue+swiper實現組件化開發(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這個數據傳遞來的 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傳來的數據 和 組件一加載節(jié)點就創(chuàng)建成功 二者不是同步,實時監(jiān)聽的swiper(傳遞的值)的變化 swiper() { this.swiperUpdate(); } }, mounted() { this.swiperUpdate(); //頁面一加載拉去數據創(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 調用的組件方法
//html <swiper :swiper="roomList.slice(6,10)" ></swiper> //js import swiper from 'components/swiper/swiper' components: { swiper },
問題:如果單純的調用_initSwiper的方法,會發(fā)現頁面是不能滾動的,但是頁面隨便修改東西,然后保存的swiper又可以滾動的,這個個原因是初始swiper的節(jié)點沒有創(chuàng)建成功,值頁面有穿進去的,一層一層的可以打印swiper的值為空的,當修改東西值就能傳遞進去的,所以的這里的我們需要通過判斷節(jié)點是否成功來update siwper的方法
相關文章
解決Vue項目中Emitted value instead of an 
這篇文章主要介紹了解決Vue項目中Emitted value instead of an instance of Error問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11