11<template >
22 <ServerError v-if =" isServerError" />
3- <RouterView v-else />
3+ <RouterView v-else-if = " isSetupRequired || isAppReady " />
44</template >
55<script lang="ts" setup>
66import {useProjectStore } from " ./pinia/useProjectStore.ts" ;
77const projectStore = useProjectStore ()
8- import {onBeforeMount , onMounted , onUnmounted , ref } from " vue" ;
8+ import {onBeforeMount , onMounted , onUnmounted , ref , watch } from " vue" ;
99import {useRoute , useRouter } from " vue-router" ;
1010import {useStatisticStore } from " ./pinia/useStatisticStore.ts" ;
1111import {useSystemConfigStore } from " ./pinia/useSystemConfigStore.ts" ;
@@ -21,11 +21,9 @@ const systemConfigStore = useSystemConfigStore()
2121import ServerError from " ./components/ServerError.vue" ;
2222const isServerError = ref (false )
2323const isSetupRequired = ref (false )
24+ const isAppReady = ref (false )
2425
2526onBeforeMount (() => {
26- // 日记项目载入后,隐藏 preloading
27- (document .querySelector (' .preloading' ) as HTMLDivElement ).style .display = ' none'
28-
2927 // 获取当前颜色模式
3028 if (window .matchMedia && window .matchMedia (' (prefers-color-scheme: dark)' ).matches ) {
3129 projectStore .colorMode = ' dark'
@@ -49,6 +47,7 @@ onMounted(()=> {
4947
5048 syncSetupStatus ()
5149 window .addEventListener (' setup-completed' , handleSetupCompleted )
50+ watch ([isServerError , isSetupRequired , isAppReady ], hidePreloadingWhenReady , {immediate: true })
5251
5352 window .addEventListener (' resize' , () => {
5453 projectStore .insets = {
@@ -86,8 +85,7 @@ function syncSetupStatus() {
8685 return
8786 }
8887
89- systemConfigStore .fetchConfig (true ).catch (() => {})
90- statisticStore .getCategoryAll ()
88+ afterBackendReady ()
9189 })
9290 .catch (() => {
9391 isServerError .value = true
@@ -97,8 +95,30 @@ function syncSetupStatus() {
9795function handleSetupCompleted() {
9896 isSetupRequired .value = false
9997 isServerError .value = false
100- systemConfigStore .fetchConfig (true ).catch (() => {})
101- statisticStore .getCategoryAll ()
98+ afterBackendReady ()
99+ }
100+
101+ async function afterBackendReady() {
102+ isAppReady .value = false
103+ try {
104+ // 类别是后续页面与统计依赖的基础数据,后端连通后优先拉取
105+ await statisticStore .getCategoryAll ()
106+ await systemConfigStore .fetchConfig (true ).catch (() => {})
107+ } finally {
108+ // 避免因单个初始化步骤异常导致页面永久停在 loading
109+ isAppReady .value = true
110+ }
111+ }
112+
113+ function hidePreloadingWhenReady() {
114+ // 使用 index.html 的 preloading,等待可渲染状态后再隐藏
115+ if (! isServerError .value && ! isSetupRequired .value && ! isAppReady .value ) {
116+ return
117+ }
118+ const preloading = document .querySelector (' .preloading' ) as HTMLDivElement | null
119+ if (preloading ) {
120+ preloading .style .display = ' none'
121+ }
102122}
103123
104124
0 commit comments