vue——事件总线($bus)

问题描述:
商品加载更多的时候,不刷新,是因为better-scroll中this.scrollerHeight刷新的时候,图片没有加载完,导致scrollerHeight的高度不是加载后的高度,解决办法是用事件总线$bus

$bus使用

  • main.js中加入Vue.prototype.$bus = new Vue()
  • 加载图片处添加
<img :src="goodItem.image" alt="" @load="imageLoad">
methods:{
 imageLoad(){
   this.$bus.$emit('itemImageLoad')
 }
}
  • 使用滚动加载处
import {debounce} from "@/common/utils"
//this.$refs尽量不要在created中使用,容易获取不到,最好是computed中使用监听
 computed() {
    const refresh = debounce(this.$refs.scroller.refresh, 200);

    this.$bus.$on("itemImageLoad", () => {
      refresh();
    });
  },
  common/utils.js
  //防抖动函数
export function debounce(func,delay){
    let timer = null;
      return function (...args) {
        if (timer) clearTimeout(timer);
        timer = setTimeout(() => {
          func.apply(this, args);
        }, delay);
      };
}
  • scroll中添加
refresh() {
  this.scroller.refresh();
},
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值