task-10(壳层/路由): 实现页面级异步组件加载边界
lazy-config 暴露 loading 延迟/超时/文案与懒加载判定;lazy-page 用 defineAsyncComponent 提供 loading/error 组件,router 对所有注册路由统一包裹; main.css 增加 .page-loading/.page-error 兜底样式。
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
|
||||
import type { AdminMenuNode } from '@/types/admin'
|
||||
import AdminLayout from '@/layout/AdminLayout.vue'
|
||||
import { useAdminSessionStore } from '@/stores/admin-session'
|
||||
import { APP_BASE_PATH } from '@/config/app'
|
||||
import { adminRouteRecords } from './routes'
|
||||
import { defineLazyPage } from './lazy-page'
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(APP_BASE_PATH),
|
||||
@@ -11,7 +12,10 @@ const router = createRouter({
|
||||
{
|
||||
path: '/',
|
||||
component: AdminLayout,
|
||||
children: adminRouteRecords,
|
||||
children: adminRouteRecords.map((record) => ({
|
||||
...record,
|
||||
component: defineLazyPage(record.component as () => Promise<{ default: unknown }>),
|
||||
})) as RouteRecordRaw[],
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
/** 异步页面加载边界可调参数(任务 10):loading 延迟/超时/文案。 */
|
||||
export const PAGE_LOADING_DELAY = 200
|
||||
export const PAGE_LOADING_TIMEOUT = 15000
|
||||
export const PAGE_LOADING_TEXT = '页面加载中'
|
||||
export const PAGE_LOAD_ERROR_TEXT = '页面加载失败,请刷新重试'
|
||||
|
||||
/** 是否为异步加载器(函数式)。 */
|
||||
export function isLazyLoader(value: unknown): value is () => Promise<unknown> {
|
||||
return typeof value === 'function'
|
||||
}
|
||||
|
||||
/** 动态 import 产物是否为含 default 的页面模块。 */
|
||||
export function isPageModule(module: unknown): boolean {
|
||||
return typeof module === 'object' && module !== null && 'default' in module
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { defineAsyncComponent, h, type Component } from 'vue'
|
||||
import {
|
||||
PAGE_LOADING_DELAY,
|
||||
PAGE_LOADING_TEXT,
|
||||
PAGE_LOADING_TIMEOUT,
|
||||
PAGE_LOAD_ERROR_TEXT,
|
||||
} from './lazy-config'
|
||||
|
||||
type PageLoader = () => Promise<{ default: unknown }>
|
||||
|
||||
/**
|
||||
* 页面级异步加载边界(任务 10):按路由分包页面,加载中/失败都有明确反馈,
|
||||
* 不把 chunk 加载错误静默吞掉。
|
||||
*/
|
||||
export function defineLazyPage(loader: PageLoader): Component {
|
||||
return defineAsyncComponent({
|
||||
loader: () => loader().then((mod) => mod.default as Component),
|
||||
delay: PAGE_LOADING_DELAY,
|
||||
timeout: PAGE_LOADING_TIMEOUT,
|
||||
loadingComponent: () => h('div', { class: 'page-loading' }, PAGE_LOADING_TEXT),
|
||||
errorComponent: () => h('div', { class: 'page-error' }, PAGE_LOAD_ERROR_TEXT),
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user