生命周期 – onBackPress() 监听页面返回
函数名 | 说明 | 平台差异说明 |
---|
onBackPress | 监听页面返回 ,返回 event = {from:backbutton、 navigateBack} ,backbutton 表示来源是左上角返回按钮或 android 返回键;navigateBack表示来源是 uni.navigateBack ;详细说明及使用:onBackPress 详解。支付宝小程序只有真机能触发,只能监听非navigateBack引起的返回,不可阻止默认行为。 | app、H5、支付宝小程序 |
onBackPress(options) {if (options.from === 'navigateBack') {return false;}}
返回刷新数据
onBackPress(options) {console.log("----------onBackPress---------", options)if (options.from === 'navigateBack') {return false;}this.back();return true;},methods: {back() {let pages = getCurrentPages(); let nowPage = pages[pages.length - 1]; let prevPage = pages[pages.length - 2]; console.log(prevPage)prevPage.isRefresh = true uni.navigateBack({ delta: 1});uni.navigateBack({ delta: 1,success:(event)=>{prevPage.$vm.getData();}});}}
export default{data(){return{isRefresh:false}},onShow() {console.log('onShow')let pages = getCurrentPages();let currPage = pages[pages.length - 1];if (currPage.isRefresh) {console.log("重新获取数据",this.isRefresh)currPage.isRefresh = false}console.log(this.isRefresh)},}