Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
783 views
in Technique[技术] by (71.8m points)

Redirected when going from "x" to "y" via a navigation guard

需求:
进入Mine页面前, 判断用户是否登录,若登录,则直接进入该页面,否则进入Login进行登录,登录成功后跳转到Mine页面。

Mine里面的代码:
I~8N[2JEVIQKPQC$QH[EK[1.png

Login里面的部分代码:
N7R~NY8GZSTWWYG0NOAF3HP.png

当我在Login组件登录成功时,报错:Error: Redirected when going from "/login?redirect=%2Fmine" to "/mine" via a navigation guard.

找了很久也没找到问题在在哪,求指导!十分感谢!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

1.终于发现问题所在,问题主要出现在Login组件中。
2.this.$store.dispatch('getUserInfo', { phone, code })为异步操作,并不会立刻执行,代码会先执行下一步this.$router.push(this.$route.query.redirect),当从Login跳转到Mine页面时,userInfo还没有请求到,因此当进入Min时会判定用户没有登录,又再次跳转到Login页面,重复的重定向引起vue-router报错。
3.解决办法也很简单

this.$store.dispatch('getUserInfo', { phone, code }).then(() => {
    this.$router.push(this.$route.query.redirect)
}).catch(() => {
    alert('登录失败!')
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...