SilurianYang / uni-simple-router

A simple, lightweight 'uni-app' routing plugin
https://v2.hhyang.cn/v2/
MIT License
749 stars 163 forks source link

建议在路由跳转时增加try ... catch 处理 #365

Closed 1261466029 closed 1 year ago

1261466029 commented 2 years ago

问题描述 由于 uni-simple-router 路由跳转前 会进行加锁逻辑,如果是非 h5 页面,将会在 next 函数执行时 自动解锁。 uniapp 在 uni.reLaunch 时,由于 onLaunch 函数报错,路由不能自动解锁,导致跳转问题。

复现步骤

#src/public/methods.js

export function lockNavjump(
    to:string|totalNextRoute|navRoute,
    router:Router,
    navType:NAVTYPE,
    forceNav?:boolean,
    animation?:uniBackApiRule|uniBackRule
):void{
    lockDetectWarn(router, to, navType, () => {
// 自动加锁逻辑
        if (router.options.platform !== 'h5') {
            router.$lockStatus = true;
        }
        navjump(to as totalNextRoute, router, navType, undefined, forceNav, animation);
    }, animation);
}

#src/public/methods.js
export function navjump(
    to:string|totalNextRoute,
    router:Router,
    navType:NAVTYPE,
    nextCall?:{
        from:totalNextRoute;
        next:Function;
    },
    forceNav?:boolean,
    animation?:uniBackApiRule|uniBackRule,
    callHook:boolean|undefined = true
) :void|never|totalNextRoute {
    if (navType === 'back') {
        let level:number = 1;
        if (typeof to === 'string') {
            level = +to;
        } else {
            level = to.delta || 1;
        }
        if (router.options.platform === 'h5') {
            (router.$route as any).go(-level);

            // Fixe  https://github.com/SilurianYang/uni-simple-router/issues/266   2021年6月3日11:14:38
            // @ts-ignore
            const success = (animation || {success: voidFun}).success || voidFun;
            // @ts-ignore
            const complete = (animation || {complete: voidFun}).complete || voidFun;
            success({errMsg: 'navigateBack:ok'});
            complete({errMsg: 'navigateBack:ok'});
            return;
        } else {
            to = backOptionsBuild(router, level, animation);
        }
    }
    const {rule} = queryPageToMap(to, router);
    rule.type = navtypeToggle[navType];
    const toRule = paramsToQuery(router, rule);
    let parseToRule = resolveQuery(toRule as totalNextRoute, router);
    if (router.options.platform === 'h5') {
        if (navType !== 'push') {
            navType = 'replace';
        }
        if (nextCall != null) { // next 管道函数拦截时 直接next即可
            nextCall.next({
                replace: navType !== 'push',
                ...parseToRule
            })
        } else {
            // Fixe  https://github.com/SilurianYang/uni-simple-router/issues/240   2021年3月7日14:45:36
            if (navType === 'push' && Reflect.has(parseToRule, 'events')) {
                if (Reflect.has(parseToRule, 'name')) {
                    throw new Error(`在h5端上使用 'push'、'navigateTo' 跳转时,如果包含 events 不允许使用 name 跳转,因为 name 实现了动态路由。请更换为 path 或者 url 跳转!`);
                } else {
                    uni['navigateTo'](parseToRule, true, voidFun, forceNav);
                }
            } else {
                (router.$route as any)[navType](parseToRule, (parseToRule as totalNextRoute).success || voidFun, (parseToRule as totalNextRoute).fail || voidFun)
            }
        }
    } else {
        let from:totalNextRoute = {path: ''};
        if (nextCall == null) {
            let toRoute = routesForMapRoute(router, parseToRule.path, ['finallyPathMap', 'pathMap']);
            toRoute = notRouteTo404(router, toRoute, parseToRule, navType);
            parseToRule = { ...toRoute, ...{params: {}}, ...parseToRule, ...{path: toRoute.path} }
            from = createToFrom(parseToRule, router);
        } else {
            from = nextCall.from;
        }
        createFullPath(parseToRule, from);
        if (callHook === false) {
            return parseToRule;
        }
        transitionTo(router, parseToRule, from, navType, HOOKLIST, function(
            callOkCb:Function
        ):void {
// 在这里报错
            uni[navtypeToggle[navType]](parseToRule, true, callOkCb, forceNav);
        })
    }
}

希望能 进行 try ... catch ... 动作,在 报错行为发生时 不影响 路由正常执行逻辑。

SilurianYang commented 2 years ago

onLaunch 什么样的情况下会报错?可否提供一个错误信息,我会在空闲时处理它