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

通过在父页面中使用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')
  },
})
相关推荐
Uyker10 小时前
微信小程序动态效果实战指南:从悬浮云朵到丝滑列表加载
前端·微信小程序·小程序
happyCoder14 小时前
uniapp 微信小程序实现定时消息订阅提醒(前后端)
微信小程序
Uyker1 天前
从零开始制作小程序简单概述
前端·微信小程序·小程序
打小就很皮...1 天前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
说私域1 天前
定制开发开源AI智能名片驱动下的海报工厂S2B2C商城小程序运营策略——基于社群口碑传播与子市场细分的实证研究
人工智能·小程序·开源·零售
说私域2 天前
内容力重塑品牌增长:开源AI大模型驱动下的智能名片与S2B2C商城赋能抖音生态种草范式
人工智能·小程序·开源·零售
前端缘梦2 天前
微信小程序登录方案实践-从账号体系到用户信息存储
前端·微信小程序
coding随想2 天前
2025年小程序开发全解析:技术储备、行业趋势与实战案例
微信小程序
Nueuis2 天前
微信小程序前端面经
前端·微信小程序·小程序
轩1152 天前
实现仿中国婚博会微信小程序
微信小程序·小程序