vue使用swiper轮播图报错TypeError: Cannot read property ‘autoplay‘ of undefined-解决方案

发现很多小伙伴用vue swiper相关的插件时,当我们想监听鼠标移入移出的时候,
使用了网上别人写的这些方法:this.$refs.mySwiper.autoplay.start()this.$refs.mySwiper.autoplay.stop(),却发现报了这个错误:typeError: Cannot read property ‘autoplay’ of undefined
然而swiper官网没有详细的说明,自行测试后发现这个是版本问题,
先查看自己的版本是多少,我的当前版本 “vue-awesome-swiper”: “^4.1.1”,
ok,我们可以打印一下,this.$refs.mySwiper 看看… 找到这个 ** $swiper或者swiperInstance**对象…

在这里插入图片描述

使用方式:

//组件:
swiper(:options="swiperOption" @mouseenter.native="onSwiperEnter"  @mouseleave.native="onSwiperLeave")
	swiper-slide  

//方法: .run()和.pause()对应,.start()和.stop()对应
 //鼠标移入
 onSwiperEnter() {
   this.$refs.mySwiper.$swiper.autoplay.stop();
 },
 
 //鼠标离开移入
 onSwiperLeave(){
   this.$refs.mySwiper.$swiper.autoplay.start();
 }
 
 //回到指定位置,第几个swiper-slide,0是第一个
 onSlideTo(){
   this.$refs.mySwiper.$swiper.slideTo(0);
 }

源码会持续更新到公众号,需要可以关注一下哦…

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值