[英]How to redirect to desired page in Vue js using vue-router?
This is my code for requiresAuth
and requiresGuest
这是我的
requiresAuth
和requiresGuest
代码
requiresAuth
: Users who satisfy this cannot go to login page or signup page ( meta for /
) requiresAuth
:满足此条件的用户不能 go 登录页面或注册页面(元为/
)
requiresGuest
: Users who satisfy this cannot go to /
or any other page having requiresAuth
meta for /login
and /signup
requiresGuest
:满足此条件的用户不能 go 到/
或任何其他具有/login
和/signup
的requiresAuth
元数据的页面
These 2 conditions are working perfectly fine for my page这两个条件对我的页面非常有效
Problem :问题:
Step-1
Lets say i have been given a url like localhost:8000/api/createapi/.......
Step-1
假设我得到了一个 url 像localhost:8000/api/createapi/.......
Step-2
So currently i am not logged in, I enter the above URL
and it redirects me to the log in
page (which is expected) Step-2
所以目前我没有登录,我输入上面的URL
并将我重定向到log in
页面(这是预期的)
Step-3
But when i log back in it redirects me to /
(which is not ideal ) Step-3
但是当我重新登录时,它会将我重定向到/
(这并不理想)
What i want :我想要什么:
After Step-2
When i log in it should redirect me automatically to localhost:8000/api/createapi/.......
Since that was the requested URL in Step-1
在
Step-2
之后,当我登录时,它应该自动将我重定向到localhost:8000/api/createapi/.......
因为那是Step-1
中请求的 URL
router.beforeEach((to, from, next) => {
// check for required auth guard
if (to.matched.some(record => record.meta.requiresAuth)) {
requiresAuthLogic(to, next, from)
} else if (to.matched.some(record => record.meta.requiresGuest)) {
requiresGuestLogic(to, next)
} else {
// Proceed to route
next()
}
})
function requiresAuthLogic (to:Route, next:Function) {
// check if NOT logged in
if (!isUserLoggedIn()) {
// Go to login
next({
path: '/login',
query: {
redirect: to.fullPath
}
})
} else if (isUserEmailVerified() === true) {
// Proceed to route
next()
}
}
function requiresGuestLogic (to:Route, next:Function) {
if (isUserLoggedIn() && isUserEmailVerified() === true) {
next({
path: '/',
query: {
redirect: to.fullPath
}
})
} else {
// Proceed to route
next()
}
}
If I've understood correctly you need to use the value which is being passed via the redirect parameter如果我理解正确,您需要使用通过重定向参数传递的值
This should be done in your login function if login is successful, you haven't shared your login function but something like this:这应该在您的登录 function 中完成,如果登录成功,您还没有共享您的登录 function 但类似这样:
loginUser() {
this.$store.dispatch('loginUser', {
email: this.email,
password: this.password
})
.then(() => {
this.$router.push(this.$route.query.redirect)
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.