一.需求:
A頁(yè)面--push到-->B頁(yè)面,在B頁(yè)面完成相關(guān)操作后pop返回上一頁(yè)面A,根據(jù)B頁(yè)面的操作結(jié)果控制返回A頁(yè)面后是否刷新
二.代碼
- A 頁(yè)面
<script>
export default {
data() {
return {
refreshIfNeeded: false
};
},
onShow() {
var pages = getCurrentPages(); // 獲取當(dāng)前頁(yè)面棧
var currentPage = pages[pages.length - 1]; // 當(dāng)前頁(yè)面
if (currentPage.data.refreshIfNeeded) {
currentPage.data.refreshIfNeeded = false;
this.refreshMethod(); // 當(dāng)前頁(yè)面 method中的方法,用來(lái)刷新當(dāng)前頁(yè)面
}
},
onLoad(option) {
},
methods: {
refreshMethod() {
}
}
};
</script>
- B 頁(yè)面
<script>
export default {
data() {
return {
};
},
onLoad(option) {
},
methods: {
updateMethod() {
update(data)
.then(res => {
console.log('修改信息 POST Success----');
uni.showToast({
title: '保存成功',
icon: 'success'
});
setTimeout(() => {
// 返回上一頁(yè)并刷新數(shù)據(jù)方法
let pages = getCurrentPages(); // 當(dāng)前頁(yè)面
let beforePage = pages[pages.length - 2]; // 上一頁(yè)
beforePage.data.refreshIfNeeded = true;
// 返回上一頁(yè) delta返回的頁(yè)面數(shù) 如果delta大于現(xiàn)有頁(yè)面數(shù),則返回首頁(yè)
uni.navigateBack({ delta: 1 });
}, 1000);
})
.catch(err => {
console.log('修改信息 POST Fail----');
console.log(err);
uni.showToast({
title: '保存失敗',
icon: 'error'
});
});
}
}
};
</script>