1. 對非首頁,使用 getCurrentPages 函數獲取當前頁面棧
onLoad: function (options) {
let pages = getCurrentPages()
if (pages.length >= 2) {
let prevpage = pages[pages.length - 2]
console.log(prevpage.route)
}
},
2. 對首頁,通過在 data 中定義標記變量,判斷頁面是否從其他頁面返回
Page({
data: {
isClose: true, //判斷當前頁面是打開還是返回頁
list: [],
page: 0
},
onLoad: function() {
this.getList()
},
getList () {
// ...
},
goDetail (e) {
this.setData({
isClose: false
})
wx.navigateTo({
url: `/pages/detail/detail`,
})
},
onShow: function () {
console.log(this.data.isClose)
if (!this.data.isClose) {
this.setData({
page: 2, // 頁碼
isClose: true,
list: []
})
this.getList()
}
},
})