微信小程序分页和下拉刷新

在page.json中配置下拉刷新和底部距顶部的距离:

复制代码
{
					"path": "school-detail",
					"style": {
						"navigationStyle": "custom",
						"navigationBarTextStyle": "black",
						"enablePullDownRefresh": true, //下拉刷新
						"onReachBottomDistance":100  //底部距顶部的距离
					}
				},

如下页面接口,需要分页的页面,在后端接口中都需要page_size(一次加载的数据条数)

复制代码
//推免信息
export const getExemption = async (id: number,pageSize: number): Promise<exemption> => {
    try {
        const res = await http<exemption>({
            method: 'POST',
            url: '/universityselectionserver/postgraduate-exemption',
            data: {
                token: memberStore.profile?.token,
                user_id: memberStore.profile?.user_id,
                college_code: 10486,
                last_id: 0,
                page_size: 10
            }
        });
        return res.data;
    } catch (error) {
        console.error("Failed to fetch major score:", error);
        return {} as exemption;
    }
}

下面是在相应页面的页面刷新和分页的代码

复制代码
const currentPage_exemption = ref(1);
const pageSize_exemption = ref(10);
const hasMore_exemption = ref(true);
//推免数据的分页加载和下拉刷新
const fetchExemption = async () => {
  if(loading.value) return;
  loading.value = true;
  try {
    const response = await getExemption(currentPage_exemption.value,pageSize_exemption.value);
    console.log('推免数据:', response);
    if(currentPage_exemption.value === 1) {
      exemptionList.value = props.infoList;
    }else {
      exemptionList.value = [...exemptionList.value,...props.infoList];
    }
    hasMore_exemption.value = props.infoList.length >= pageSize_exemption.value;
  }catch (error) {
    console.error('获取推免数据失败', error);
  }finally {
    loading.value = false;
    uni.stopPullDownRefresh();
  
  };
}
onMounted(() => {
  fetchNotice();
  fetchExemption();
});
onPullDownRefresh(() => {
  currentPage.value = 1;
  hasMore.value = true;
  currentPage_exemption.value = 1;
  hasMore_exemption.value = true;
  fetchNotice();
  fetchExemption();
})
onReachBottom(() => {
  if(hasMore.value) {
    currentPage.value++;
    fetchNotice();
    
  }
  if(hasMore_exemption.value) {
    currentPage_exemption.value++;
    fetchExemption();
  }
})
相关推荐
寧笙(Lycode)5 分钟前
OpenTelemetry 入门
前端
猪哥帅过吴彦祖8 分钟前
Flutter 系列教程:列表与网格 - `ListView` 和 `GridView`
前端·flutter·ios
用户352120195608 分钟前
React hooks (useRef)
前端
Mintopia12 分钟前
⚡当 Next.js 遇上实时通信:Socket.io 与 Pusher 双雄传
前端·后端·全栈
tangdou36909865515 分钟前
可怕!我的Nodejs系统因为日志打印了Error 对象就崩溃了😱 Node.js System Crashed Because of Logging
前端·javascript·后端
Takklin18 分钟前
Vue 与 React 应用初始化机制对比 - 前端框架思考笔记
前端·react.js
Mintopia21 分钟前
🎨 数据增强技术在 AIGC 训练中的应用:提升 Web 生成的多样性
前端·javascript·aigc
华仔啊21 分钟前
如何用 Vue3 打造高级音乐播放器?进度条+可视化效果,代码简洁可复用!
前端·css·vue.js
小傅哥22 分钟前
新项目完结,Ai Agent 智能体、拖拉拽编排!
前端·后端
ttod_qzstudio29 分钟前
解决 Vue 3 + TypeScript 中 v-for 循环类型推断问题
前端·vue.js·typescript