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

通过在父页面中使用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')
  },
})
相关推荐
Brave & Real1 天前
小程序 const 在js中以及与同类的var和let之间的差异
javascript·微信小程序·小程序
Joolun商城源码_Java1 天前
JooLun Pro旗舰版SaaS多租户商城:商城小程序与店铺小程序的功能区别详解
小程序
0的0次方1 天前
从0到1:如何运营一款支付宝证件照小程序(含避坑指南)
小程序·新媒体运营
小郑加油1 天前
第16天:综合训练——数据去重
小程序
海兰2 天前
【小程序】 贪吃蛇(Next.js+WebSocket+SQLite + Prisma ORM)
javascript·websocket·小程序
路光.2 天前
uniapp小程序/App使用webview打通麦克风权限实现录音功能
小程序·uni-app·app
hnxaoli2 天前
统信小程序(十四)支持拖拽的旋图程序
python·小程序
2501_915106322 天前
深入解析HTTPS抓包原理、中间人攻击及反抓包技术攻防
数据库·网络协议·ios·小程序·https·uni-app·iphone
silvia_Anne2 天前
微信小程序商品列表
微信小程序·小程序
游戏开发爱好者82 天前
React Grab工具详解:AI助力Vue3、Svelte和Solid前端元素调试
android·ios·小程序·https·uni-app·iphone·webview