微信小程序页面刷新实例

本文介绍了一种使用getCurrentPages()函数刷新小程序页面数据的方法,通过在onShow()函数中调用此函数,可以实现页面数据的实时更新。同时,提供了一个在数据删除后自动刷新页面的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

页面刷新
小程序提供了getCurrentPages()函数获取页面栈。
在Onshow()函数里写如下代码

    if (getCurrentPages().length != 0) {
      //刷新当前页面的数据
      getCurrentPages()[getCurrentPages().length - 1].onLoad()
    }

示例1

      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function() {
        //判断是否有打开过页面
        this.getlist();
        if (getCurrentPages().length != 0) {
          //刷新当前页面的数据
          getCurrentPages()[getCurrentPages().length - 1].onLoad()
        }
      },

数据请求函数

getlist: function(options) {
    let that = this;
    wx.request({
      url: app.globalData.url + '/myvote',
      method: 'POST',
      data: {
        id: that.data.creator
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        that.setData({
          info: res.data
        })
      }
    })
  },

示例2(删除后自动刷新),在需要自动刷新的地方调用一次onShow函数

  sureDelete(e) {
    var that = this;
    console.log(e.currentTarget.dataset.id)
    wx.request({
      url: app.globalData.url+'/myvote/deleteone',
      data: {
        id: e.currentTarget.dataset.id
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        that.setData({
          modalName: null
        })
        that.onShow();        //此处调用onShow函数刷新页面
      }
    })
  },
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值