问题描述:
商品加载更多的时候,不刷新,是因为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();
},