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