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和访问方式需要一致。
这两种方式即使过多的重定向后,也能正常跳转至首页。
目前我不知道这是什么原因,如果有知道原理的,麻烦指教下~~