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

通过在父页面中使用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')
  },
})
相关推荐
走,带你去玩4 小时前
uniapp 微信小程序水印
微信小程序·小程序·uni-app
是一碗螺丝粉4 小时前
🔥 微信H5视频自动播放终极秘籍:WeixinJSBridge竟是官方“通行证”?
微信小程序
一笑code5 小时前
vue/微信小程序/h5 实现react的boundary
微信小程序·vue·react
菌菇汤7 小时前
uni-app实现单选,多选也能搜索,勾选,选择,回显
前端·javascript·vue.js·微信小程序·uni-app·app
CC同学呀14 小时前
从0到100:单位订餐统计小程序开发日记2025
小程序
666HZ66619 小时前
微信小程序中scss、ts、wxml
微信小程序·小程序·scss
二十十十十十19 小时前
微信点餐小程序—美食物
微信·小程序
向明天乄20 小时前
在小程序中实现实时聊天:WebSocket最佳实践
websocket·网络协议·小程序
h1853859224420 小时前
租车小程序电动车租赁小程序php方案
小程序
海的诗篇_20 小时前
前端开发面试题总结-原生小程序部分
前端·javascript·面试·小程序·vue·html