vue+swiper實(shí)現(xiàn)組件化開發(fā)的實(shí)例代碼
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這個(gè)數(shù)據(jù)傳遞來的 methods: { _initSwiper() { this.mySwiper = new Swiper('.swiper-container', { autoplay: 5000,//可選選項(xiàng),自動(dòng)滑動(dòng) }) }, _updateSwiper() { this.$nextTick(() => { this.mySwiper.update(true); //swiper update的方法 }) }, swiperUpdate() { if (this.mySwiper) { //節(jié)點(diǎn)存在 this._updateSwiper(); //更新 } else { this._initSwiper(); //創(chuàng)建 } }, }, watch: { //通過props傳來的數(shù)據(jù) 和 組件一加載節(jié)點(diǎn)就創(chuàng)建成功 二者不是同步,實(shí)時(shí)監(jiān)聽的swiper(傳遞的值)的變化 swiper() { this.swiperUpdate(); } }, mounted() { this.swiperUpdate(); //頁(yè)面一加載拉去數(shù)據(jù)創(chuàng)建節(jié)點(diǎn) } } </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的方法,會(huì)發(fā)現(xiàn)頁(yè)面是不能滾動(dòng)的,但是頁(yè)面隨便修改東西,然后保存的swiper又可以滾動(dòng)的,這個(gè)個(gè)原因是初始swiper的節(jié)點(diǎn)沒有創(chuàng)建成功,值頁(yè)面有穿進(jìn)去的,一層一層的可以打印swiper的值為空的,當(dāng)修改東西值就能傳遞進(jìn)去的,所以的這里的我們需要通過判斷節(jié)點(diǎn)是否成功來update siwper的方法
相關(guān)文章
Vue 級(jí)聯(lián)下拉框的設(shè)計(jì)與實(shí)現(xiàn)
在前端開發(fā)中,級(jí)聯(lián)選擇框是經(jīng)常用到的,這樣不僅可以增加用戶輸入的友好性,還能減少前后端交互的數(shù)據(jù)量。本文就介紹一下使用Vue實(shí)現(xiàn)級(jí)聯(lián)下拉框,感興趣的可以了解一下2021-07-07vue項(xiàng)目中使用tinymce編輯器的步驟詳解
本文分步驟給大家介紹了vue項(xiàng)目中使用tinymce編輯器的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09基于Vue2.0+ElementUI實(shí)現(xiàn)表格翻頁(yè)功能
Element UI 是一套采用 Vue 2.0 作為基礎(chǔ)框架實(shí)現(xiàn)的組件庫(kù),它面向企業(yè)級(jí)的后臺(tái)應(yīng)用,能夠幫助你快速地搭建網(wǎng)站,極大地減少研發(fā)的人力與時(shí)間成本。這篇文章主要介紹了Vue2.0+ElementUI實(shí)現(xiàn)表格翻頁(yè)功能,需要的朋友可以參考下2017-10-10vue簡(jiǎn)單實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng)
這篇文章主要為大家詳細(xì)介紹了vue簡(jiǎn)單實(shí)現(xiàn)轉(zhuǎn)盤抽獎(jiǎng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03解決Vue項(xiàng)目中Emitted value instead of an 
這篇文章主要介紹了解決Vue項(xiàng)目中Emitted value instead of an instance of Error問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Vue中Object.assign清空數(shù)據(jù)報(bào)錯(cuò)的解決方案
這篇文章主要介紹了Vue中Object.assign清空數(shù)據(jù)報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Vue網(wǎng)頁(yè)html轉(zhuǎn)換PDF(最低兼容ie10)的思路詳解
這篇文章主要介紹了Vue網(wǎng)頁(yè)html轉(zhuǎn)換PDF(最低兼容ie10)的思路詳解,實(shí)現(xiàn)此功能需要引入兩個(gè)插件,需要的朋友可以參考下2017-08-08