国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

vue+swiper實現組件化開發的實例代碼

2019-11-19 15:04:18
字體:
來源:轉載
供稿:網友

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"http://    ]   }  },  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) { //節點存在     this._updateSwiper(); //更新    } else {     this._initSwiper(); //創建    }   },  },  watch: {   //通過props傳來的數據 和 組件一加載節點就創建成功 二者不是同步,實時監聽的swiper(傳遞的值)的變化   swiper() {    this.swiperUpdate();   }  },  mounted() {   this.swiperUpdate(); //頁面一加載拉去數據創建節點  } }</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的方法,會發現頁面是不能滾動的,但是頁面隨便修改東西,然后保存的swiper又可以滾動的,這個個原因是初始swiper的節點沒有創建成功,值頁面有穿進去的,一層一層的可以打印swiper的值為空的,當修改東西值就能傳遞進去的,所以的這里的我們需要通過判斷節點是否成功來update siwper的方法

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汕头市| 慈利县| 丰台区| 大名县| 巩留县| 安乡县| 乡宁县| 蒙阴县| 柘城县| 左云县| 天台县| 海门市| 集贤县| 新营市| 长泰县| 深圳市| 大安市| 民丰县| 万州区| 莱阳市| 平顺县| 滕州市| 安阳县| 台北市| 章丘市| 哈巴河县| 齐齐哈尔市| 来凤县| 平定县| 吴旗县| 扎鲁特旗| 友谊县| 家居| 乌恰县| 铜鼓县| 克什克腾旗| 尚义县| 雷州市| 增城市| 广昌县| 孟州市|