切換頁(yè)面刷新:通過(guò)onShow()便可實(shí)現(xiàn)
返回上一頁(yè)通過(guò)uni.navigateBack({delta: 1});實(shí)現(xiàn)
以返回上一頁(yè)刷新為例
從B頁(yè)面返回上一頁(yè)到A頁(yè)面
在A頁(yè)面寫入方法refreshHandler()
methods: {
// 執(zhí)行刷新邏輯
refreshHandler() {
uni.request({
url: getApp().globalData.position + 'Produce/select_producting',
data: {
username: getApp().globalData.username
},
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: 'POST',
dataType: 'json',
success: res => {
this.info = res.data.info;
},
fail(res) {
console.log("查詢失敗")
}
})
}
},
在A頁(yè)面寫入onLoad(),onShow()
?onLoad():首次進(jìn)入頁(yè)面時(shí)執(zhí)行的方法,這里直接引入refreshHandler() 可以直接查詢到頁(yè)面數(shù)據(jù)
onShow():進(jìn)入頁(yè)面執(zhí)行的方法,主要切換到本頁(yè)面就會(huì)執(zhí)行該方法,這里也是直接引入refreshHandler()函數(shù),可以在每次進(jìn)入頁(yè)面時(shí),執(zhí)行數(shù)據(jù)刷新(寫入onShow(),便可解決進(jìn)入頁(yè)面不刷新的問(wèn)題,返回上一頁(yè)刷新的問(wèn)題也會(huì)解決)
//剛進(jìn)入頁(yè)面執(zhí)行的操作
onLoad() {
this.refreshHandler()
},
//進(jìn)入頁(yè)面這行的操作
onShow() {
this.refreshHandler()
}
在B頁(yè)面執(zhí)行返回A頁(yè)面的操作
methods: {
is_back(e) {
console.log('返回')
uni.showToast({
title: '成功',
})
setTimeout(function() {
//返回上一頁(yè)
uni.navigateBack({
delta: 1,
});
}, 500)
},
},
核心
uni.navigateBack({
????????delta: 1,
?});
這段代碼的作用是返回上一個(gè)頁(yè)面,delta參數(shù)指定了返回的層數(shù)。在這里,delta為1表示返回上一層(即當(dāng)前頁(yè)面的前一個(gè)頁(yè)面)。
注:uni.navigateBack函數(shù)是由uni-app框架提供的,用于在多頁(yè)面應(yīng)用中返回上一個(gè)頁(yè)面。它類似于瀏覽器的后退按鈕功能。
如果你希望執(zhí)行該函數(shù)后刷新上一個(gè)頁(yè)面,你可以使用下面的方式:
當(dāng)成功返回上一個(gè)頁(yè)面后,會(huì)調(diào)用上一個(gè)頁(yè)面的onLoad
生命周期函數(shù)進(jìn)行刷新操作。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-612168.html
uni.navigateBack({
? ? delta: 1,
? ? success: function () {
? ? ? ? const pages = getCurrentPages(); //獲取當(dāng)前頁(yè)面棧
? ? ? ? const prevPage = pages[pages.length - 1]; //獲取上一個(gè)頁(yè)面實(shí)例對(duì)象
? ? ? ? prevPage.onLoad(); //調(diào)用上一個(gè)頁(yè)面的onLoad方法
? ? }
});文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-612168.html
到了這里,關(guān)于uni-app:切換頁(yè)面刷新,返回上一頁(yè)刷新(onShow鉤子函數(shù)的使用)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!