vue 检查登录的接口,在页面登录跳转后会再次请求 造成了死循环?-灵析社区

通了顺畅了

我有一个检查登录接口`checked/login` 要求必须放在App.vue页面 在`mounted`方法中我请求了检查登录接口, 在打开登录页面`/web/login`时会调用一次,正常登录后 后台返回的字段data let data = { errcode: 0, is_login: true, isAdmin: false, msg: "获取成功", info: {status: 4} } 根据 `data.info.status === 4` 判断跳转到`/web/index`页面, 但是在`/web/index`页面也会请求一次`checked/login`接口,然后会再次根据 `data.info.status === 4` 判断跳转到`/web/index`页面,这样就陷入了一个一直刷新的局面,该怎么解决???

阅读量:362

点赞量:16

问AI
用路由守卫 router.beforeEach((to, from, next) => { if (to.path !== '/web/login') { // 检查登录状态 axios.post('checked/login').then(response => { let data = response.data; if (data.info.status === 4) { next('/web/index'); } else { next(); } }); } else { next(); } }); 检查当前路由: mounted() { if (this.$route.path !== '/web/index') { // 请求检查登录接口 axios.post('checked/login').then(response => { let data = response.data; if (data.info.status === 4) { this.$router.push('/web/index'); } }); } }