vue-router — Uncaught (in promise) Error: Redirected from “/login” to “/” via a navigation guard

后端 未结 6 1514
孤城傲影
孤城傲影 2020-12-11 14:38

Why is vue-router giving me this error? To be clear, the login flow works as intended but I want to a) get rid of the errro and b) understand why the error is happening.

6条回答
  •  清歌不尽
    2020-12-11 15:22

    I have the same error. This error created by router.push("/"); row - it trying to say you that pushing to home was interrupted by redirection in navigation guard. But actually, it's not an error because it is an expected behaviour.

    I made ignoring of such errors by the following way:

    const router = new VueRouter({
        mode: 'history',
        routes: _routes,
    });
    
    /**
     * Do not throw an exception if push is rejected by redirection from navigation guard
     */
    const originalPush = router.push;
    router.push = function push(location, onResolve, onReject) {
        if (onResolve || onReject) {
            return originalPush.call(this, location, onResolve, onReject);
        }
        return originalPush.call(this, location).catch((err) => {
            let reg = new RegExp('^Redirected when going from "[a-z_.\\/]+" to "[a-z_.\\/]+" via a navigation guard.$');
            if (reg.test(err.message)) {
                // If pushing interrupted because of redirection from navigation guard - ignore it.
                return Promise.resolve(false);
            }
            // Otherwise throw error
            return Promise.reject(err);
        });
    };
    

提交回复
热议问题