uni-simple-router icon indicating copy to clipboard operation
uni-simple-router copied to clipboard

没有上级页面时,返回拦截不起作用

Open jiangmaniu opened this issue 3 years ago • 4 comments

问题描述 [问题描述:尽可能简洁清晰地把问题描述清楚]

页面在唤起 Modal 时,页面会自动拦截用户的返回操作,用于关闭该 Modal,该操作在有上级页面时,可以正常操作,但是若无上级页面,则插件直接会报 不存在的页面栈,请确保有足够的页面可用,当前 level:1 错误,貌似 判断是否有上级页面 的优先级会比 返回拦截的回调 要高

demo.zip

jiangmaniu avatar Dec 29 '21 02:12 jiangmaniu

在没有上级页面时,用户的返回操作不会触发 onBackPressbeforeRouteLeave 回调,而是直接会报错并触发 routerErrorEach 回调

jiangmaniu avatar Dec 29 '21 02:12 jiangmaniu

当页面栈不如返回时,可以认为用户将要选择推出,所以不会触发 onBackPressbeforeRouteLeave 后面会尝试优化插件

退出应用

SilurianYang avatar Dec 29 '21 05:12 SilurianYang

@SilurianYang 感谢你的回复!我想在页面上显示了 Modal 弹窗时,如果用户执行返回操作时,则优先关闭弹窗,待弹窗全部关闭后,在做退出应用的操作,而不是直接退出程序。但目前的 demo 只能在有上级页面的时候才能实现优先关闭弹窗阻止路由返回,如果没有上的级页面,就无法优先关闭弹窗,因为它不会触发 onBackPressbeforeRouteLeave 回调。

jiangmaniu avatar Dec 29 '21 06:12 jiangmaniu

退出应用 有文档描述了如何调用页面的方法,你可以改写改写,例如:

// xxx.vue
<script>
export default {
    methods: {
        runtimeQuit(){
            return new Promise(resolve=>{
            uni.showModal({
                title: '提示',
                content: '您确定要退出应用吗?',
                success: function (res) {
                    if (res.confirm) {
                        resolve();
                    } 
                }
            });
        })
        }
    }
}
</script>
// router.js
const router = createRouter({
    platform: process.env.VUE_APP_PLATFORM,  
    routerErrorEach:async ({type,level,...args})=>{
        console.log({type,level,...args})
        // #ifdef APP-PLUS
        if(type===3){
            router.$lockStatus=false;
            const pages=getCurrentPages();
            const {$vm}=pages[pages.length-1];
            await $vm.runtimeQuit();
            plus.runtime.quit();
        }
        // #endif
    },
    routes: [...ROUTES]
});

SilurianYang avatar Dec 29 '21 07:12 SilurianYang