From fba17f188d4b8a770a681ab3d174366237c3a510 Mon Sep 17 00:00:00 2001 From: xiaoxian521 <1923740402@qq.com> Date: Thu, 1 Dec 2022 16:06:05 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20`initRouter`=20?= =?UTF-8?q?=EF=BC=8C=E5=85=BC=E5=AE=B9=20`sso`=20=E5=9C=BA=E6=99=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/utils.ts | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/router/utils.ts b/src/router/utils.ts index ba81ad2..127b738 100644 --- a/src/router/utils.ts +++ b/src/router/utils.ts @@ -184,29 +184,34 @@ function handleAsyncRoutes(routeList) { addPathMatch(); } -/** 初始化路由 */ +/** 初始化路由(`new Promise` 写法防止在异步请求中造成无限循环)*/ function initRouter() { - return new Promise(resolve => { - if (getConfig()?.CachingAsyncRoutes) { - // 开启动态路由缓存本地sessionStorage - const key = "async-routes"; - const asyncRouteList = storageSession.getItem(key) as any; - if (asyncRouteList?.length > 0) { + if (getConfig()?.CachingAsyncRoutes) { + // 开启动态路由缓存本地sessionStorage + const key = "async-routes"; + const asyncRouteList = storageSession.getItem(key) as any; + if (asyncRouteList && asyncRouteList?.length > 0) { + return new Promise(resolve => { handleAsyncRoutes(asyncRouteList); - } else { + resolve(router); + }); + } else { + return new Promise(resolve => { getAsyncRoutes().then(({ data }) => { handleAsyncRoutes(data); storageSession.setItem(key, data); + resolve(router); }); - } - resolve(router); - } else { + }); + } + } else { + return new Promise(resolve => { getAsyncRoutes().then(({ data }) => { handleAsyncRoutes(data); resolve(router); }); - } - }); + }); + } } /**