refactor: 使用router.push替代window.location.href,实现SPA无刷新跳转
问题:
- 使用window.location.href会导致整页刷新,影响用户体验
- Vue应用应使用router.push实现单页应用内的路由跳转
修复:
1. http.js:
- 导入router实例
- 401错误:router.push('/login')
- 403错误:router.push('/login')
2. AuthService.js:
- 导入router实例
- logout函数:router.push('/login')
效果:
- 页面跳转更流畅,用户体验更佳
- 保持应用状态,避免不必要的重新加载
- 符合Vue单页应用的最佳实践
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
|
||||
import { createClientAxios } from '@gold/api/axios/client'
|
||||
import authService from '@/services/AuthService'
|
||||
import router from '@/router'
|
||||
|
||||
/**
|
||||
* 创建应用层 HTTP 客户端实例
|
||||
@@ -32,8 +33,8 @@ export function createHttpClient(options = {}) {
|
||||
await authService.handleAuthError(
|
||||
error,
|
||||
() => {
|
||||
// 刷新失败,跳转到登录页
|
||||
window.location.href = '/login'
|
||||
// 刷新失败,使用router跳转,避免整页刷新
|
||||
router.push('/login')
|
||||
}
|
||||
)
|
||||
// 如果刷新成功,AuthService返回null,不跳转,不抛出错误
|
||||
@@ -44,8 +45,8 @@ export function createHttpClient(options = {}) {
|
||||
if (on403) {
|
||||
on403(error)
|
||||
} else {
|
||||
console.warn('403权限不足,跳转到登录页')
|
||||
window.location.href = '/login'
|
||||
console.warn('403权限不足,使用router跳转到登录页')
|
||||
router.push('/login')
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user