vue使用swiper的坑-刷新不动,loop属性失效

在网上查了很多问题,原因应该是加载顺序的问题,数据还没有完全加载的时候就已经渲染swiper了。

解决办法,在swiper初始化的时候把以下两个属性加上。

observer:true,//修改swiper自己或子元素时,自动初始化swiper
observeParents:true,//修改swiper的父元素时,自动初始化swiper

就可以了。

下面贴完整代码,有兴趣的可以看一下。

<template>
  <div class="main_box">
    <swiper :options="swiperOptionTwo" ref="mySwiper">
        <!-- slides -->
        <swiper-slide>
          <div id="index-page">
            <img class="index-bg" src="../assets/images/index_bg.png">
            <img class="share-button" src="../assets/images/shoes.png" v-on:click="toShoes">
            <router-link to="/rule"><div class="rule-click div-click"></div></router-link>
            <swiper :options="swiperOption" ref="mySwiper">
              <!-- slides -->
              <swiper-slide>
                <div v-for="(value,index) in marathonList" class="main-info">
                  <div class="marathon-box">
                    <img @click="goTms(value.coverLink)" class="index-click" src="../assets/images/index-click-img_03.png">
                    <div class="box-info">
                      <div v-for="(valueMode,index) in value.marathonList" class="box-info-line">
                        <p class="info-text"><span>{{valueMode.name}}</span><span class="value"> {{valueMode.count}}</span></p>
                        <img class="heart" src="../assets/images/heart.png">
                        <p class="info-text"><span>{{valueMode.consume}}</span>人已申请</p>
                      </div>
                      <div class="box-info-line">
                        <p class="info-text bottomFont"><span>申请时间</span><span class="value bottomFont"> {{value.applyTime}}</span></p>
                        <p class="info-text bottomFont"><span>开奖时间</span><span class="value bottomFont"> {{ value.luckTime | moment("MMM Do YY") }}</span></p>
                      </div>
                    </div>
                  </div>
                  <button class="white-button" v-if="value.applyStatus == 0" @click="goApply(value.name)">立即申请</button>
                  <button class="white-button" v-if="value.applyStatus == 1">坐等开奖</button>
                  <button class="white-button" v-on:click="luck(value.name)" v-if="true">点击开奖</button>
                  <button class="white-button" v-if="value.applyStatus == 4">未中奖</button>
                  <button class="white-button" v-if="value.applyStatus == 3">中奖</button>
                  <button class="white-button" v-if="value.applyStatus == 5">已结束</button>
                </div>
              </swiper-slide>
              <swiper-slide>
                <div class="main-info">
                  <div v-for="(value,index) in marathonList" class="marathon-box">
                    <img v-on:click="goTms" class="index-click" src="../assets/images/index-click-img_03.png">
                    <div class="box-info">
                      <div v-for="(valueMode,index) in value.marathonList" class="box-info-line">
                        <p class="info-text"><span>{{valueMode.name}}</span><span class="value"> {{valueMode.count}}</span></p>
                        <img class="heart" src="../assets/images/heart.png">
                        <p class="info-text"><span>{{valueMode.consume}}</span>人已申请</p>
                      </div>
                      <div class="box-info-line">
                        <p class="info-text bottomFont"><span>申请时间</span><span class="value bottomFont"> {{value.applyTime}}</span></p>
                        <p class="info-text bottomFont"><span>开奖时间</span><span class="value bottomFont"> {{ value.luckTime | moment("MMM Do YY") }}</span></p>
                      </div>
                    </div>
                  </div>
                </div>
              </swiper-slide>
              <div class="swiper-pagination" slot="pagination"></div>
              <div class="swiper-button-prev" slot="button-prev"></div>
              <div class="swiper-button-next" slot="button-next"></div>
            </swiper>
          </div>
        </swiper-slide>
        <swiper-slide>
          <!--slide2-->
          <div class="slideTwo">
            <!-- <img class="index-bg" src="../assets/images/bottom_bg.png"> -->
            <div class="bottom-top">
              <p>跑马训练计划</p>
              <img src="../assets/images/card_test.png">
              <img src="../assets/images/card_top.png">
            </div>
            <div class="bottom-mid">
              <p>跑马干货</p>
              <img src="../assets/images/card_test.png">
              <img src="../assets/images/card_mid.png">
            </div>
            <div class="bottom-bot">
              <p>咕咚学院</p>
              <img src="../assets/images/card_test.png">
              <img src="../assets/images/card_bottom.png">
            </div>
          </div>
        </swiper-slide>
    </swiper>
    <img src="../assets/images/tip.png" style="width: 110px; height: 59px; margin-bottom: 5px; left: 320px;" id="array" class="fixed">
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
#index-page {
  position: relative;
  .swiper-container {
    position: absolute;
      height: 1003px;
      width: 100%;
      top: 200px;
  }
  .swiper-button-next {
    background-image: url(../assets/images/next-btn.png);width: 25px;
    height: 635px;
    right: 0px;
    background-size: cover;
    top: 15%;
  }
}
.swiper-container {
  position: absolute;
    height: 1217px;
    width: 100%;
}
.slideTwo {
  height: 1217px;
  color: #fff;
  p {
    position: absolute;
    margin-top: 60px;
    margin-left: 23px;
    color: #b8b8b8;
    font-size: 24px;
  }
  img {
    position: absolute;
    margin-top: 100px;
    left: 21px;
    display: block;
  }
  .bottom-top {
    position: relative;
    height: 400px;
  }
  .bottom-mid {
    position: relative;
    height: 400px;
  }
  .bottom-bot {
    position: relative;
    height: 400px;
  }
}
.swiper-button-prev {
  background-image: url(../assets/images/next-btn.png);
  height: 635px;
  left: 0px;
  background-size: cover;
  top: 15%;
}
  @-webkit-keyframes tipmove{0%{bottom:10px;opacity:0}50%{bottom:15px;opacity:1}100%{bottom:20px;opacity:0}}
  .ani{
    position:absolute;
  }
  #array{
    -webkit-animation: tipmove 1.5s infinite ease-in-out;
  }
  .fixed{
    position:absolute;
    z-index:999;
    width: 125px;
    height: 70px;
    margin-bottom: 13px;
    left: 320px;
  }
  .share-button {
    position: absolute;
    top: 0px;
    right: 20px;
  }
  .rule-click {
    position: absolute;
    top: 20px;
    left: 40px;
    width: 150px;
    height: 60px;
  }
  .text-link {
    margin-top: 40px;
    text-decoration: underline;
    color: #858585;
    font-size: 28px;
  }
  .bottomFont {
    font-size: 20px !important;
  }

  .white-button {
    display: block;
    margin-top: 70px;
    background-color: #ffffff;
    color: #000000;
    padding: 20px;
    width: 400px;
    border-radius: 40px;
    margin-left: 135px;
    font-weight: 400;
  }
  .main-info {
    position: absolute;
    top: 100px;
    width: 670px;
    left: 50%;
    transform: translate3D(-50%, 0, 0);
    -webkit-transform: translate3D(-50%, 0, 0);

    .marathon-box {
      background-color: #ffffff;
      color: #a2a2a2;
      font-size: 28px;

      .box-info {
        padding: 0 50px 40px 50px;
      }
      .box-info-line {
        text-align: left;
        .info-text {
          display: inline-block;
          padding: 40px 0px 0px 0px;
          min-width: 240px;
        }
      }

      .index-click {
        display: block;
      }

      .value {
        font-size: 29px;
        color: #292929;
      }
    }
  }
  .index-bg {
    position: relative;
    display: block;
  }
</style>
<script type="text/ecmascript-6">
  import store from ‘../store/index‘;
  import api from ‘../assets/script/api‘;
  import codoonNativeBridge from ‘codoon-native-bridge-js‘
  import { swiper, swiperSlide } from ‘vue-awesome-swiper‘
  import {mapGetters} from ‘vuex‘;
  import moment from ‘vue-moment‘

  export default{
    components: {
      swiper,
      swiperSlide
    },
    data(){
      return {
        progress: 0,
        temp: 0,
        store: store,
        isInCodoon: false,
        swiperOption: {
          slidesPerView: ‘auto‘,
          centeredSlides: true,
          paginationClickable: true,
          spaceBetween: 30,
          prevButton: ‘.swiper-button-prev‘,
          nextButton: ‘.swiper-button-next‘,
          loop: true,
          notNextTick: true,
          observer:true,//修改swiper自己或子元素时,自动初始化swiper
          observeParents:true,//修改swiper的父元素时,自动初始化swiper
        },
        swiperOptionTwo: {
          direction : ‘vertical‘,
          speed: 800,
          slidesPerView: ‘auto‘,
          centeredSlides: true,
          paginationClickable: true,
          spaceBetween: 30,
          notNextTick: true,
          observer:true,//修改swiper自己或子元素时,自动初始化swiper
          observeParents:true,//修改swiper的父元素时,自动初始化swiper
        },
        swiperIndex: 0,
        marathonList: [],
        share: this.$route.query.share,
      }
    },
    computed: {
      // swiper() {
      //   return this.$refs.mySwiper.swiper;
      // }
    },
    mounted() {
      var vm = this;
      api.marathonList({}).then(response => {
          if(response.status) {
            vm.marathonList = response.data.marathonList;
          }
      });
      // this.$refs.mySwiper.swiper;
      // this.swiper();
    },
    computed: {
        ...mapGetters([
            ‘userInfo‘
        ])
    },
    methods: {
      swiper() {
        return this.$refs.mySwiper.swiper;
      },
      goTms(url) {
        var nativeB = new codoonNativeBridge();
        nativeB.jumpNative({
          type: ‘webView‘,
          value: url
        });
      },
      toShoes() {
        store.router.push({name: ‘shoes‘});
      },
      luck(name) {
        store.router.push({name: ‘luck‘, query: {name: name}});
      },
      goApply(name) {
        store.router.push({name: ‘select‘, query: { type: this.type, name: name }});
      }
    }
  }
</script>
时间: 2024-08-09 02:21:13

vue使用swiper的坑-刷新不动,loop属性失效的相关文章

vue使用swiper轮播组件开启loop模式时的问题总结

最近在vue项目中使用了swiper插件来完成轮播功能,没有开启循环模式,一切都很顺利 具体怎么在vue项目中引入swiper插件,这个太简单就不提了,上代码 html <div class="swiper-container"> <div class="swiper-wrapper"> <template v-if='banner.length > 0'> <div class="swiper-slide&

vue单页应用前进刷新后退不刷新方案探讨

引言 前端webapp应用为了追求类似于native模式的细致体验,总是在不断的在向native的体验靠拢:比如本文即将要说到的功能,native由于是多页应用,新页面可以启用一个的新的webview来打开,后退其实是关闭当前webview,其上一个webview就自然显示出来:但是在单页的webapp应用中,所有内容其实是在一个页面中展示的,不存在多页的情况,这时就需要前端开发来想办法实现相应的体验效果. 首先需要说明一下,本文所说的前进刷新后退不刷新是指组件是否重新渲染,比如列表A页面,点击

Vue+WebSocket 实现页面实时刷新长连接

Vue+WebSocket 实现页面实时刷新长连接:https://www.cnblogs.com/fmixue/p/9110074.html 看完让你彻底搞懂Websocket原理:https://blog.csdn.net/frank_good/article/details/50856585 webSocket长连接实现demo(场景:扫码自动跳转登录,或者替换轮询):https://blog.csdn.net/GordoHu/article/details/78293803 原文地址:h

vue 首次加载缓慢/刷新后加载缓慢 原因及解决方案

# vue 首次加载缓慢/刷新后加载缓慢 原因及解决方案 最近做项目发现一个问题,页面每次刷新后加载速度都非常慢,20s左右,在开发环境则非常流畅,几乎感觉不到,本文参考望山的各种方案优化 1,关闭打包时生成的map文件 在config/index.js文件中讲productionSourceMap设置为false,再次打包便没有了map文件 2,vue-router路由懒加载 懒加载的实现方式有很多种,这里简单说三种实现方法 vue异步组件 import() webpack的require.e

vue 组件传值踩坑日记 1

今天在用平时很少用到的传值方式,是V2.4以后新加入属性$attrs $listener 以及inheritAttrs. 总结:这样形式的代码适合套娃模式的组件传递,却不适合兄弟组件的传值,那样的传值方式,需要创建一个事件总线,说白了就是新new一个空的vue,详见,我的第二篇日志<vue 组件传值踩坑日记 2> 废话不多说,直接上代码,大家可以边看注释边测试一下效果吧 <!DOCTYPE html> <html lang="en"> <hea

vue配置环境踩坑

Vue 环境配置踩坑 目录 Vue 环境配置踩坑 windows下cnpm -v :无法将"cnpm"项识别为 cmdlet.函数.脚本文件或可运行程序的名称. windows下cnpm -v :无法将"cnpm"项识别为 cmdlet.函数.脚本文件或可运行程序的名称. npm和cnpm都是在git的全局环境下下载的 且安装完cnpm,nodejs文件夹下就已经有了node_cache.node_global这两个文件夹: 而且在用户变量里path变量路径已经存在

vue使用swiper

下载swiper npm install swiper -s 在main.js引入 import  Swiper  form  'swiper' import  'swiper/dist/css/swiper.min.css' Vue.use(Swiper); 使用的话,在mounted中使用 原文地址:https://www.cnblogs.com/chenyudi/p/12695994.html

vue中遇到的坑 --- 变化检测问题(数组相关)

最近在项目中遇到了一个问题,不知道为什么,所以最后通过动手做demo实践.查文档的方式解决了,这里做一个总结. 例1 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>vue</title> <script src="https://unpkg.com/[email protected]/d

前端Vue框架 02 斗篷指令:v-cloak, 指令: 属性指令:v-bind, 表单指令:v-model, 条件指令:v-show v-if, 循环指令:v-for

1.斗篷指令(了解) """ v-cloak:避免屏幕闪烁 1)属性选择器,会将v-cloak属性所在的标签隐藏 2)当vue环境加载后,会将v-cloak属性解析移除,所以内容{{ num }}就会显示出来 3)而现在vue已经准备完毕,所以用户会直接看到数值10,而不会看到 页面从{{ num }}闪烁成数值10 """ <style> [v-cloak] { display: none; } </style> <