待解决:vue后台项目,chrome重定向过多,跳转失败

vue-element框架,启动后,使用localhost+端口方式,在chrome进行访问首页,自动跳转到了登录(此时无token,跳到登录是正常操作)。
在输入用户名和密码之后,调登录接口成功了,但f12显示一直在重定向,页面也无法正常调转到首页。

同样的代码,使用ie打开没问题,使用ip+端口的方式打开也没问题。项目上线也很久了,使用正常。我这次是在此项目上进行新功能。

排查1:

目前排查出来,是在调登录接口成功之后,token的处理有问题。因为只有在token不存在的时候才会一直跳到登录页面。
结合vuex使用的,这里有存token,但
在这里插入图片描述

在这里插入图片描述

hasRoutes为false,这里又把token删掉了,我注掉这行代码,虽然token在,但依旧反复重定向,跳转失败。
使用localhost时,只输出了2,使用ip方式时先输出2后输出1

router.beforeEach(async (to, from, next) => {
  NProgress.start()
  document.title = getPageTitle(to.meta.title)
  const hasToken = getToken()
  if (hasToken) {
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next({ path: '/' })
      NProgress.done()
    } else {
      const hasRoutes = store.getters.routes && store.getters.routes.length > 0
      if (hasRoutes) {
        console.log(1)
        next()
      } else {
        console.log(2)
        try {
          const routes = await store.dispatch('permission/generateRoutes')
          // console.log(routes)
          router.addRoutes(routes)
          // next({ ...to, replace: true })
          next({ path: '/' })
        } catch (error) {
          console.log(error)
          removeToken()
          next(`/login?redirect=${to.path}`)
        }
      }
    }
  } else {
    /* has no token*/
    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next()
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      next(`/login?redirect=${to.path}`)
      NProgress.done()
    }
  }
})

排查2

在封装request请求的时候,有配置baseUrl的地址,当baseURL: 'http://ip:8081/payment',时,只能通过ip+端口的方式访问页面当baseURL: 'http://localhost:8081/payment',只能通过localhost+端口方式访问。即baseurl和访问方式需要一致。
这两种方式即使过多的重定向后,也能正常跳转至首页。

目前我不知道这是什么原因,如果有知道原理的,麻烦指教下~~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值