feat(admin): 增加启动与导航反馈并保护工作标签状态
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { isNavigationFailure, NavigationFailureType, type RouteLocationNormalized, type Router } from 'vue-router'
|
||||
|
||||
/**
|
||||
* Observe the navigation, never decide it. Keep one lightweight bar across redirects
|
||||
* and overlapping guards; a late failure for an older route doesn't own the bar.
|
||||
*/
|
||||
export function installNavigationFeedback(router: Router, doc: Document = document,
|
||||
notify?: (failedPath?: string) => void) {
|
||||
let owner: RouteLocationNormalized | undefined
|
||||
let timer: ReturnType<typeof setTimeout> | undefined
|
||||
let bar: HTMLElement | undefined
|
||||
|
||||
function stop() {
|
||||
clearTimeout(timer)
|
||||
timer = undefined
|
||||
owner = undefined
|
||||
bar?.remove()
|
||||
bar = undefined
|
||||
}
|
||||
|
||||
function owns(to: RouteLocationNormalized) {
|
||||
return owner === to || (to.redirectedFrom && to.redirectedFrom === (owner?.redirectedFrom ?? owner))
|
||||
}
|
||||
|
||||
router.beforeEach(to => {
|
||||
if (doc.getElementById('admin-boot')) return
|
||||
notify?.()
|
||||
owner = to
|
||||
if (timer !== undefined || bar) return
|
||||
timer = setTimeout(() => {
|
||||
timer = undefined
|
||||
bar = doc.createElement('div')
|
||||
bar.id = 'admin-route-progress'
|
||||
bar.setAttribute('role', 'progressbar')
|
||||
bar.setAttribute('aria-label', '正在打开页面')
|
||||
doc.body.appendChild(bar)
|
||||
}, 180)
|
||||
})
|
||||
router.afterEach((to, _from, failure) => {
|
||||
// Duplicates bypass beforeEach, including redirects back to the current route.
|
||||
if (owns(to) || isNavigationFailure(failure, NavigationFailureType.duplicated)) stop()
|
||||
})
|
||||
router.onError((error, to) => {
|
||||
if (owns(to)) {
|
||||
stop()
|
||||
// Page assets failed after the app started. Don't reload over an
|
||||
// unsaved form or mistake a background request for a route error.
|
||||
notify?.(to.fullPath)
|
||||
}
|
||||
console.error('路由导航失败:', error)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import { shallowRef } from 'vue'
|
||||
|
||||
// Ephemeral UI state. Navigation targets are issued only by the existing router.
|
||||
export const failedRoute = shallowRef<string>()
|
||||
export function reportRouteLoad(path?: string) { failedRoute.value = path }
|
||||
Reference in New Issue
Block a user