@2890594972
2018-05-25T07:14:00.000000Z
字数 768
阅读 2726
1511c
当我们想去某个页面,但是因为需要登录,才能去。
如果登录后,如何重定向到之前想去的那个页面。
/* 统一拦截器开始 */
router.beforeEach((to, from, next) => {
if(to.meta.requireAuth){// 代表需要登录
//判断有没有登录
if(sessionStorage.getItem('userInfo')){//有值,代表登录过了
next();
}else{
next({
path:'Login',
query: {
redirect: to.fullPath
}
});
}
}else{
next();
}
})
/* 统一拦截器结束 */
//登录请求
login(){
/* 专门用来写请求逻辑 */
const params = new URLSearchParams();
params.append('cellphone', this.cellphone);
params.append('password', this.password);
login(params).then(res=>{
const data = res.data;
if(data.code === 200){
//正常登录
alert('登录成功');
// 存数据
sessionStorage.setItem('userInfo',JSON.stringify(data.data));
// this.$router.push({path:'Mine'});
console.log(this.$route.query);
this.$router.push(this.$route.query.redirect || '/Mine')
}else{
alert(data.data);
}
})
}