首先,通過下面代碼告訴編譯器要編譯哪些頁面
static modules = import.meta.glob('./views/**/*.vue');
然后動態加載函數這樣寫:
static asyncLoadView = (path: string) => {return defineAsyncComponent({loader: <any>Global.modules[`./views/${path}.vue`],loadingComponent: LoadingView, // 加載中顯示的組件errorComponent: ErrorLoadView, // 加載出錯顯示的組件delay: 500, // 在顯示loadingComponent組件之前, 等待多長時間timeout: 10000, // 加載超時的時間(毫秒)/*** err: 錯誤信息,* retry: 函數, 調用retry嘗試重新加載* attempts: 記錄嘗試的次數*/onError: (err, retry, fail, attempts) => {if (attempts < 2) {window.setTimeout(retry, 1000);}else {fail();}}});}