onLoad() {
window.history.replaceState(null, null, document.URL)
window.addEventListener('popstate', this.browserBack)
},
onUnload() {
window.removeEventListener("popstate", this.browserBack);
},
methods: {
browserBack() {
// 在这里写弹框
uni.showModal({
title: '提示',
content: '返回后此页面的操作将不作保留!',
cancelText: '确定返回',
confirmText: '留在此页',
success: (res) => {
if (res.confirm) {
// 用户选择留在此页,不进行任何操作
} else if (res.cancel) {
window.history.back(); // 使用window.history.back()返回上一页
}
}
});
window.history.replaceState(null, null, document.URL); // 保留此行代码
},
}
uniappH5中监听手机自带物理返回键
于 2023-07-14 16:29:47 首次发布