About the login interception of vue

main.js Code:

//
router.beforeEach((to, from, next) => {
    let myCookie = global.getCookie("token");
    if (!myCookie && to.path != "/login"){
        router.push("/login");
    }
    else{
        next();
    }
});

No matter which page you click, as long as you are not logged in, you will jump to the login page. This is no problem

then changed the requirements, so you can see it even if you don"t log in on the home page, so I change it like this

//
router.beforeEach((to, from, next) => {
    let myCookie = global.getCookie("token");
    if (!myCookie && to.path != "/"){
        router.push("/login");
    }
    else{
        next();
    }
});

as a result, the browser reported an error. What is the reason for this?

clipboard.png

May.07,2022

router.beforeEach((to, from, next) => {
    let myCookie = global.getCookie('token');
    if (!myCookie && to.path != '/'){
        next({
          path: '/login'  // token
        })
    }
    else{
        next();
    }
});

the next of beforeEach global guard must be written


you are caught in a wireless loop error. The symbol & & indicates that both conditions must be met before it can be satisfied. When you jump to / login , there is no value for cookie , and to.path is also / login so it executes route.push ('/ login') , so it loops in the wireless loop. Logic error

Menu