hookehuyr

refactor(router): 重构路由加载逻辑以优化用户体验

移除Suspense组件,改为在路由守卫中统一管理加载状态
添加路由错误处理确保加载状态正确重置
...@@ -7,19 +7,8 @@ ...@@ -7,19 +7,8 @@
7 --> 7 -->
8 <template> 8 <template>
9 <div id="app"> 9 <div id="app">
10 - <!-- 路由懒加载时的占位,避免白屏 --> 10 + <router-view />
11 - <router-view v-slot="{ Component }"> 11 + <!-- 全局loading蒙版 -->
12 - <Suspense>
13 - <component :is="Component" />
14 - <template #fallback>
15 - <div class="global-loading-overlay">
16 - <van-loading type="spinner" size="24px" color="#FFF" text-color="#FFF">加载中...</van-loading>
17 - </div>
18 - </template>
19 - </Suspense>
20 - </router-view>
21 -
22 - <!-- 全局loading蒙版(接口进行中显示) -->
23 <div v-if="is_loading" class="global-loading-overlay"> 12 <div v-if="is_loading" class="global-loading-overlay">
24 <van-loading type="spinner" size="24px" color="#FFF" text-color="#FFF">加载中...</van-loading> 13 <van-loading type="spinner" size="24px" color="#FFF" text-color="#FFF">加载中...</van-loading>
25 </div> 14 </div>
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
6 * @Description: 文件描述 6 * @Description: 文件描述
7 */ 7 */
8 import { createRouter, createWebHashHistory } from 'vue-router' 8 import { createRouter, createWebHashHistory } from 'vue-router'
9 +import { useLoadingStore } from '@/stores/loading.js'
9 10
10 const routes = [ 11 const routes = [
11 { 12 {
...@@ -72,12 +73,47 @@ router.beforeEach((to, from, next) => { ...@@ -72,12 +73,47 @@ router.beforeEach((to, from, next) => {
72 document.title = to.meta.title 73 document.title = to.meta.title
73 } 74 }
74 75
76 + // 开启全局loading用于覆盖路由懒加载白屏
77 + /**
78 + * 开启路由级加载指示
79 + * @description 进入任意页面前先展示全局loading,避免异步组件/资源加载期间出现白屏
80 + */
81 + try {
82 + const loading = useLoadingStore()
83 + loading.start()
84 + } catch (e) {
85 + // 兜底:忽略异常,保证路由流程继续
86 + void e
87 + }
88 +
75 // 这里可以添加权限验证逻辑 89 // 这里可以添加权限验证逻辑
76 next() 90 next()
77 }) 91 })
78 92
79 -router.afterEach(() => { 93 +// 路由解析完成(异步组件与守卫均已完成)后关闭loading
80 - // 路由跳转后的逻辑 94 +/**
95 + * 关闭路由级加载指示
96 + * @description 在组件解析完成后关闭一次全局loading,剩余由接口加载计数继续控制
97 + */
98 +router.beforeResolve(() => {
99 + try {
100 + const loading = useLoadingStore()
101 + loading.end()
102 + } catch (e) {
103 + // 兜底:忽略异常,保证路由流程继续
104 + void e
105 + }
106 +})
107 +
108 +// 路由错误时重置loading,避免蒙版残留
109 +router.onError(() => {
110 + try {
111 + const loading = useLoadingStore()
112 + loading.reset()
113 + } catch (e) {
114 + // 兜底:忽略异常,保证路由流程继续
115 + void e
116 + }
81 }) 117 })
82 118
83 export default router 119 export default router
......