下面是关于在 vue中使用better-scroll 组件的时候 需要下来加更多不止一次加载场景
/*
* pullingUp: 监听的应该是,元素滚动过程中,被滚动的那个元素的底边与父级外框底边的距离,如果达到某个值,就执行其回调函数;
* finishPullUp: 这个类似控制一个开关,比如在触发pullingUp事件的时候,插件肯定会把一个开关给关掉,防止用户重复上拉
* 在数据加载完成以后,需要执行finishPullUp()把开关打开,以便下次可以继续执行上拉刷新;
* refresh:其实就是重新计算一下内容的高度和宽度,也许也会计算外框的高度和宽度,因为dom结构发生了变化了,所以宽高要从新计算
* 以为整个插件基本上都是在计算一些距离差值,所以肯定需要从新计算的;
*/
如下业务场景
通过子组件往父组件传递 下来加载信息 然后父组件 调用子组件 再次使用下来加载 调用接口返回数据
自己封装一个scroll.vue 文件 里面引用 better-scroll 组件(子组件)
<template>
<div class="wrapper" ref="wrap">
<div class="content">
<slot></slot>
</div>
</div>
</template>
<script>
import BScroll from "better-scroll";
export default {
name: "Scroll",
props: {
probeType: {
type: Number,
default: 0,
},
pullUpLoad: {
type: Boolean,
default: true,
},
},
data() {
return {
scroll: null,
};
},
mounted() {
this.scroll = new BScroll(this.$refs.wrap, {
probeType: this.probeType,
pullUpLoad: this.pullUpLoad,
click: true,
});
// 监听滚动的位置
this.scroll.on("scroll", (position) => {
this.$emit("scroll", position);
});
// 监听上拉事件 上拉加载更多
this.scroll.on("pullingUp", () => {
// 子组件往父组件传值
this.$emit("pullingUp");
});
},
methods: {
scrollTo(x, y, time = 500) {
// 向上箭头的设置
this.scroll.scrollTo(x, y, time);
},
finishPullUp(){
// 不止一次加载更多
this.scroll.finishPullUp()
}
},
};
</script>
<style scoped>
</style>
父组件
<template>
<div id="home">
<scroll
class="content"
ref="scroll"
@scroll="contentScroll"
:probe-type="3"
:pull-up-load="true"
@pullingUp="loadMore"
>
<swiper :banner="banner"></swiper>
<recommend-view :recommends="recommends"></recommend-view>
<feature-view></feature-view>
<tab-control
class="control"
:titles="titles"
@tabClick="tabClick"
></tab-control>
<good-list :goods="showGoods"></good-list>
</scroll>
<back-top @click.native="topClick" v-show="topTrue"></back-top>
</div>
</template>
<script>
import TabControl from "components/content/tabControl/TabControl.vue";
import { getHomeMultidata, getHomeGoods } from "network/home";
import GoodList from "components/content/goods/GoodList.vue";
import Scroll from "components/common/scroll/Scroll.vue";
import BackTop from "components/content/backTop/BackTop.vue";
export default {
name: "Home",
components: {
TabControl,
GoodList,
Scroll,
Scroll,
BackTop,
},
data() {
return {
banner: [],
recommends: [],
titles: [],
goods: {
pop: {
page: 0,
list: [
{}
],
},
new: {
page: 0,
list: [
{}
],
},
sell: {
page: 0,
list: []
},
this.getHomeGoods("pop");
this.getHomeGoods("new");
this.getHomeGoods("sell");
},
computed: {
showGoods() {
return this.goods[this.curretnType].list;
},
},
methods: {
/*
网络请求事件 自己封装网络请求,方便调用数据和使用
*/
getHomeMultidata() {
getHomeMultidata().then((res) => {
this.banner = res.data.banner;
this.recommends = res.data.recom;
});
},
getHomeGoods(type) {
const page = this.goods[type].page + 1;
getHomeGoods(type).then((res) => {
this.goods[type].list.push(...res.data.list);
this.goods[type].page += 1;
// 通过调用子组件的方法来使用 多次加载
this.$refs.scroll.finishPullUp();
});
},
/*
点击事件
*/
tabClick(index) {
switch (index) {
case 0:
this.curretnType = "pop";
break;
case 1:
this.curretnType = "new";
break;
case 2:
this.curretnType = "sell";
break;
}
},
// 事件监听
topClick() {
// this.$refs.scroll 可以访问子组件中的里面的任何东西
this.$refs.scroll.scroll.scrollTo(0, 0, 500);
},
contentScroll(position) {
this.topTrue = Math.abs(position.y) > 800;
},
loadMore() {
// 通过 curretnType 来判断 点击类型 然后进行数据请求 并且 页数加1
this.getHomeGoods(this.curretnType);
},
},
};
</script>