通过url传参的方式实现页面回跳

74 篇文章 3 订阅

实现步骤:
1.在响应拦截器跳转时添加参数
window.location.href = '#/login?returuurl=' + window.location.hash.substr(1)
2.在登录成功时判断是否携带参数,是则返回传参页
应用场景:用户浏览页面需要进行登录时,为避免找不回浏览记录,在登录后返回登录前浏览的页面。

响应拦截器

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    // console.log(response);
    if (response.data.statusCode == 401 || response.data.statusCode == 403) {
        Toast.fail(response.data.message)

        console.log(window.location.hash);//    #/articleDetail/1
        console.log(window.location.hash.substr(1));//    /articleDetail/1
        // 拿到哈希值,截取需要的部分传入url
        //returuurl:代表键,参数是?后面键=值
        window.location.href = '#/login?returuurl=' + window.location.hash.substr(1)
    }
    return response;
}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});

跳转成功后url效果
在这里插入图片描述

登录页

 console.log(window.location.hash); // 哈希值: #/login?returuurl=%2FarticleDetail%2F10
 console.log(window.location.hash.split("=")[1]); // 截取需要的哈希值: %2FarticleDetail%2F10
 console.log(decodeURIComponent(window.location.hash.split("=")[1])); // 解码后: /articleDetail/10
 let url = decodeURIComponent(window.location.hash.split("=")[1]);
 if (url && url != "undefined") {
   this.$router.push({ path: url });
 } else {
   this.$router.push({ path: `/personal/${res.data.data.user.id}` });
 }
        }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值