页面刷新
小程序提供了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函数刷新页面
}
})
},