优化路由跳转之一

108 阅读1分钟

在没登陆时点击“我的订单”,会进入login组件,在用户登录后点击登录应该直接跳转到原本想去的组件

image.png

1.将想去没去的路由存储到query参数中

else {
    // 未登录不能去交易相关页面,支付相关,个人中心
    // to.path.indexOf('/center')!=-1表示跳转的to.path字符串包含center字段
if (to.path == '/trade' || to.path == '/pay' || to.path == '/paysuccess' || to.path.indexOf('/center')!=-1)     {
        // 将想去没去的路由存储到query参数中
        next('/login?redirect=' + to.path)
    } else {
        next();
    }
}

image.png 2.在登录组件中修改登录事件

取出route的query参数进行判断

有:直接跳转到query参数指定的路由

没有:跳转到home

let topath = this.$route.query.redirect || "/home";
if (this.phone && this.password) {
    this.$router.push(topath)
}

这时候点击登录就会直接跳转到我的订单页面。

感谢支持!