vue中用户未登录跳转到登录页,登录后返回将要去的页面或者之前的页面


1.全局拦截

// response全局拦截
axios.interceptors.response.use(
    response => {
        if(response.data.status == 1001){
            router.push({
                path:'/login'
                // 状态码不对,跳转登录页面时,需要传一个域名在登录页判断
                // 为了防止分享登录页面
                query:{redirect:location.hostname}
            })
        }
        return response;
    },
    error => {
        return Promise.reject(error.response.data)
    }
)

 

2.类目初始化接口

// 类目初始化接口
initProductType().then(response =>{
    if(response.data.status == 1000){
        this.initdata = response.data.productTypeResults
    }else{
        this.$router.push({path:'/login',query:{redirect:location.hostname}})
    }
})

 

3.这样在登录页就拿到了上个页面的location.hostname了。然后在登录页判断一下

if(this.$router.query.redirect == location.hostname){
    this.$router.go(-1)
}else{
    this.$router.push({path:'/index'})
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM