微信小程序 如何在组件中实现 上拉加载下一页和下拉触底

通过在父页面中使用selectComponent来调用子组件的方法来实现

1、在component中配置好方法

子页面homePage/index/index.js
javascript 复制代码
// homePage/index/index.js
var total = 0
var pageNo = 1
const pageSize = 20
Component({
  /**
   * 组件的属性列表
   */
  properties: {},
  lifetimes: {
    created: function () {
    },
    attached: function () {
      setTimeout(() => {
        this.getProfessorQuestionList('fresh')
      }, 0)
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    questionList:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    loadData(action) {
      if (action == 'fresh') {
        pageNo = 1
      }
      if (action == 'more') {
        if (pageNo * pageSize < total) {
          pageNo++
          this.getProfessorQuestionList('more')
        }
      } else {
        this.getProfessorQuestionList('fresh')
      }
    },
    getProfessorQuestionList(type) {
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      httpUtils.post({
        url: url,
        data: {
          page: pageNo,
          pageSize: pageSize,
        }
      }).then(res => {
        wx.hideLoading()
        total = res.page.total;
        let questionList = stringUtils.isNull(res.list) ? [] : res.list;
        if (type == 'fresh') {
          this.setData({
            questionList: questionList
          })
        } else {
          this.setData({
            questionList: this.data.questionList.concat(questionList)
          })
        }
      }).finally(() => {
        wx.hideLoading()
         this.setData({
          noData:this.data.questionList.length? true:false
        })
      })
    },
  }
})

2、在父页面使用selectComponent来调用子组件的方法

父页面page/index/index.json
javascript 复制代码
{
  "usingComponents": {
    "homePage": "../../homePage/index/index",
  },
  "navigationStyle": "custom"
}
父页面page/index/index.wxml
javascript 复制代码
 <homePage id="homePage" />
父页面page/index/index.js
javascript 复制代码
Page({
  data: {
    homePage: ''
  },

  onReady() {
    this.setData({
      homePage: this.selectComponent("#homePage"),
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.data.homePage.loadData('fresh')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.data.homePage.loadData('more')
  },
})
相关推荐
Greg_Zhong14 分钟前
学习AI 工程师第 3 天:小程序中调用豆包模型,实现ai助手(打字机效果与流式输出)
小程序·ai工程师·小程序调用豆包实现ai助手
于先生吖41 分钟前
家政派单小程序定制厂家
大数据·小程序
00后程序员张1 小时前
完整指南 iOS App上架到App Store的步骤详解
macos·ios·小程序·uni-app·objective-c·cocoa·iphone
hhzz2 小时前
记录微信小程序tabbar不显示问题:uni-app Vue 3 自定义 tabBar 不渲染
vue.js·微信小程序·uni-app
久爱@勿忘2 小时前
uniappH5跳转小程序
前端·小程序·uni-app
Greg_Zhong2 小时前
微信小程序中调用豆包【免费】模型,实现小程序版ai助手完整版
微信小程序·豆包模型·调用豆包模型封装ai助手
307617 小时前
uni-app在微信小程序国际化分包方案:优雅解决主包体积超限问题
微信小程序
文慧的科技江湖1 天前
光储充一体化开源能源管理系统 需求说明书(简单版) - 慧知开源充电桩平台
小程序·开源·能源·光储充·光伏系统·实现光储充全设备统一监控·光储充一体化开源能源管理系统
eric*16881 天前
Mac反编译小程序教程
小程序·小程序反编译
打瞌睡的朱尤1 天前
微信小程序50~75
微信小程序·小程序