Vue routing login verification problem

clipboard.png
home

clipboard.png


according to the logic you wrote, did not log in, jumped to the login page, but still did not log in, continue to skip the login page and then loop through the stack
to determine whether to log in when to = login, do not judge whether or not to log in


because every time you enter the landing page, you will judge that you have not logged in and jump into the landing page again to form an endless loop.
you can write

like this.
vueRouter.beforeEach(function (to, from, next) {
    const nextRoute = [ 'account', 'order', 'course'];
    const auth = store.state.auth;
    //3
    if (nextRoute.indexOf(to.name) >= 0) {
        //
        if (!store.state.auth.IsLogin) {
            vueRouter.push({name: 'login'})
        }
    }
    //
    if (to.name === 'login') {
        if (auth.IsLogin) {
            vueRouter.push({name: 'home'});
        }
    }
    next();


judge that if you are not logged in and are not currently on the login page, jump to the login page

Menu