首页 > 解决方案 > Vue路由器 - 克服下一个({})未捕获(承诺)未定义错误

问题描述

我随机收到关于使用next({ path: 'dfdfd' }).

我认为该错误正在Uncaught (in promise) undefined阻止我的脚本的其余部分运行。

我已经尝试了网络上的所有选项,但所有选项都涉及使用$router.pushnot next()

next({path: '/profile'}, () => {});
next({path: '/profile'}).catch(()=> {});
try { next({path: '/profile'}) } catch(err){}

不要做任何事。

谁能告诉我如何发现这个错误?

问候

编辑。

以上用于从 vue-routersbeforeRouteEnterbeforeRouteUpdate方法调用的 axios 请求的错误处理。

/**
 * Error Handler
 * @param error
 * @param next
 * @param to
 * @param from
 */
errorHandler(error, next, to, from) {
    switch(error.response.status) {
        case 401:
            if(from.path !== '/login') {
                next({path: '/login'});
            } else {
                next();
            }
        break;
        case 498:
            for(let i in error.response.data.errors) {
                db.commit('setNotifications', []);
                db.commit('addErrorNotification', error.response.data.errors[i][0]);
            }
            if(from.path !== '/profile') {
                next({path: '/profile'}); // where the error is thrown
            } else {
                next();
            }
        break;
        default:
            next();
        break;
    }
    db.commit('setPageLoading', false);
}

标签: vue.jsvue-router

解决方案


推荐阅读