vue实战-轮播图的最佳方案/swiper的使用

vue实战-轮播图的最佳方案/swiper的使用

1.swiper的使用

1)下载安装插件swiper

vue2项目最好不要用swiper的最新版本,不兼容。

npm i swiper@5 --save
2)在轮播图组件中引入swiper
import Swiper from 'swiper'
3)在入口文件main.js中引入swiper样式

因为不知一处用到轮播图,直接在入口文件引入一次即可。

import "swiper/css/swiper.css"
4)在swiper官网复制swiper实例
new Swiper ('.swiper-container', {
        loop: true, // 循环模式选项
        
        
        // 如果需要分页器
        pagination: {
          el: '.swiper-pagination',
          clickable :true
        },
        
        // 如果需要前进后退按钮
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev',
        },
        
        // 如果需要滚动条
        scrollbar: {
          el: '.swiper-scrollbar',
        },
    })        
5)问题:为什么swiper实例在mounted中不能直接书写?

因为在运行swiper实例之前,它的页面结构必须以及存在且完整,而此时由于轮播图的数据需要从store仓库中获得需要花费时间,所以此时的页面结构是不完整的,所以在mounted中书写没有效果。

6)解决方法1

在mounted中使用setTimeout来实现过一段时间后执行,可以解决这个问题,但是不完美。

setTimeout(()=>{
      new Swiper ('.swiper-container', {
        loop: true, // 循环模式选项
        
        
        // 如果需要分页器
        pagination: {
          el: '.swiper-pagination',
          clickable :true
        },
        
        // 如果需要前进后退按钮
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev',
        },
        
        // 如果需要滚动条
        scrollbar: {
          el: '.swiper-scrollbar',
        },
    })        
     },1000)

2.轮播图最佳方案(watch+nectTick)

1)watch

监听bannerList数据的变化,只要state中从空数组变为非空数组就能监听到
但是这个只能保证bannerList的数据以及有了,不能保证页面组件中的v-for以及循环结束,所以还需要nectTick。

2)nectTick

定义:在下次DOM更新循环结束之后执行延迟回调,在修改数据之后,立即使用这个方法,获取更新后的DOM。
同时满足两个“之后”才执行。它保证了页面中的结构一定存在且完整
所以最终的watch+nectTick组合使用才能实现轮播图的最佳效果。

  watch:{
    bannerList:{
      handler(){
        this.$nextTick(()=>{
          new Swiper ('.swiper-container', {
                loop: true, // 循环模式选项
                
                
                // 如果需要分页器
                pagination: {
                  el: '.swiper-pagination',
                  clickable :true
                },
                
                // 如果需要前进后退按钮
                navigation: {
                  nextEl: '.swiper-button-next',
                  prevEl: '.swiper-button-prev',
                },
                
                // 如果需要滚动条
                scrollbar: {
                  el: '.swiper-scrollbar',
                },
            })        
        })
      }
    }
  }
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用:npm ERR! 404 Not Found - GET https://registry.npmmirror.com/@vue/vue-loader-v15 - [NOT_FOUND] @vue/vue-loader-v15 not found vue 安装npm i element-ui -S 等 组件 报错 npm ERR! code E404 npm ERR! 404 Not Found - GET https://registry.npmmirror.com/@vue/vue-loader-v15 - [NOT_FOUND] @vue/vue-loader-v15 not found npm ERR! 404 npm ERR! 404 '@vue/vue-loader-v15@15.10.0' is not in the npm registry. npm ERR! 404 You should bug the author to publish it (or use the name yourself!) npm ERR! 404 It was specified as a dependency of '@vue/cli-service' npm ERR! 404 npm ERR! 404 Note that you can also install from a npm ERR! 404 tarball, folder, http url, or git url. 根据引用的内容,这个错误是由于找不到vue-awesome-swiper的tarball数据所导致的。vue-awesome-swiper的tarball数据位于http://172.168.251.67:4873/vue-awesome-swiper/-/vue-awesome-swiper-3.1.3.tgz。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [npm install -g cnpm --registry=https://registry.npm.taobao.org报错](https://blog.csdn.net/qq_36853469/article/details/99900961)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [ist的matlab代码-gitlab-uberspace-tutorial:如何在https://uberspace.de上安装GitLab](https://download.csdn.net/download/weixin_38543120/19078868)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [ 404 Not Found - GET https://registry.npmmirror.com/@vue%2fvue-loader-v15 - [NOT_FOUND] @vue](https://blog.csdn.net/qq_51307593/article/details/127484795)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值