problem description
wrote a demo of vue-router and wanted to implement:
- enter the page and first redirect to
/ index
and then there is a< router-link to= "/ login" > to login < / router-link >
Click to jump to the/ login
page; - but I wrote in the global guard that when
to.paht ="/ login"
, I skip to the front page vianext ("/")
; - normally, it will be skipped to the front page and should not go to the
/ login
page; - but now you can enter, and when you use
http://localhost:8080/-sharp/login
, the second time you type a link in the address bar, you will automatically jump to/ index
but once and still enter/ login
;
related codes
router.js routing
{
path: "/",
redirect: "/index"
},
{
path: "/index",
name: "index",
component: Index
},
{
path: "/login",
name: "login",
component: Login
},
{
path: "/404",
name: "404",
component: PageError
},
{
path: "*",
redirect: "/404"
}
Global Guard in main.js
router.beforeEach((to, form, next) => {
console.log("-beforeEach-", to.path)
if (to.path === "/login") {
console.log("-login-")
next("/")
}
next()
}
data output by console
-beforeEach- /index
-beforeEach- /login
-login-
in theory, the output of -login-
should trigger next ("/")
, and then enter the guard output -beforeEach- /
and enter / index
, but the guard is not actually triggered again.
the environmental background of the problems and what methods you have tried
- try to change
next ("/")
tonext ("/ a")
then you can successfully enter the/ 404
page; - but enter
http://localhost:8080/-sharp/login
via url to enter the/ login
page; - tries to find a rule, that is, when the path of the current page is the same as
next ("/")
, the jump will fail;