this.$nextTick解决swiper初始化过早问题,

解决swiper初始化过早问题有以下方法:

(1)组件法:

  • 给组件加“ : key=状态.length”;
  • 给组价加:“ v-if=‘状态.length’  ”;
  • 在组件mounted生命周期函数里进行初始化;

(2)指令法:

  • inserted生命周期函数里进行初始化;

(3)this.$nextTick法:在生命周期函数updated后面执行,且只执行一次,当状态再更新就不会执行了,所以进行swiper初始化非常合适,解决了在updated里会多次初始化的问题:

写法:

    this.$nextTick(() => {
                        console.log("我比updated执行的都晚,而且只执行一次")
                        new Swiper(".swiper", {
                            // direction: 'vertical', // 垂直切换选项
                            loop: true, // 循环模式选项

                            // 如果需要分页器
                            pagination: {
                                el: '.swiper-pagination',
                            }
                        })

                    })

完整代码: 

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>


    <link rel="stylesheet" href="./swiper/swiper-bundle.min.css">
    <script src="./swiper/swiper-bundle.min.js"></script>
    <script src="./lib/vue.js"></script>

    <style>
        /* .swiper {
            width: 600px;
            height: 300px;
            background-color: yellow;
        } */
    </style>
</head>

<body>
    <div id="box">
        <header>导航</header>

        <div class="swiper">
            <div class="swiper-wrapper">
                <!-- <div class="swiper-slide">11111</div>
                <div class="swiper-slide">22222</div>
                <div class="swiper-slide">33333</div> -->
                <div class="swiper-slide" v-for="data in datalist" :key="data">
                    <img :src="data" alt="">
                </div>
            </div>

            <!-- 如果需要分页器 -->
            <div class="swiper-pagination"></div>

        </div>

        <footer>底部内容</footer>
    </div>

    <script>
        new Vue({
            el: "#box",
            data: {
                datalist: []
            },

            mounted() {
                //ajax请求回来的数据
                setTimeout(() => {
                    //后端的返回的数据用数组接收
                    this.datalist = ["https://static.maizuo.com/pc/v5/usr/movie/e856bdc65507b99800f22e47801fa781.jpg",
                        "https://p0.pipi.cn/mmdb/25bfd6ddb53c7e5015d23c5bc24d876c03d41.jpg?imageView2/1/w/160/h/220",
                        "https://p0.pipi.cn/mmdb/25bfd692ddd7e13139313954ee2938728ad6a.jpg?imageView2/1/w/160/h/220"]

                    this.$nextTick(() => {
                        console.log("我比updated执行的都晚,而且只执行一次")
                        new Swiper(".swiper", {
                            // direction: 'vertical', // 垂直切换选项
                            loop: true, // 循环模式选项

                            // 如果需要分页器
                            pagination: {
                                el: '.swiper-pagination',
                            }
                        })

                    })
                }, 2000)



            },
            // updated(){
            //     new Swiper(".swiper", {
            //         // direction: 'vertical', // 垂直切换选项
            //         loop: true, // 循环模式选项

            //         // 如果需要分页器
            //         pagination: {
            //             el: '.swiper-pagination',
            //         }
            //     })
            // }  
        })
    </script>
</body>

</html>

结果:

 

缺点是:没有复用性 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陌一一

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值