From 0eb8d884724c96cfa32e0043d518470b1fa93b37 Mon Sep 17 00:00:00 2001 From: wanchun <445436867@qq.com> Date: Wed, 29 Mar 2023 16:35:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8D=A2=E6=9B=B4=E5=90=88=E7=90=86?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/route/template/routeExports.tpl | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/packages/fes-preset-built-in/src/plugins/route/template/routeExports.tpl b/packages/fes-preset-built-in/src/plugins/route/template/routeExports.tpl index d32f294a..8c94fe41 100644 --- a/packages/fes-preset-built-in/src/plugins/route/template/routeExports.tpl +++ b/packages/fes-preset-built-in/src/plugins/route/template/routeExports.tpl @@ -39,37 +39,37 @@ export const createRouter = (routes) => { let isInit = false router.beforeEach(async (to, from, next) => { - if(!isInit) { - isInit = true - const beforeRenderConfig = plugin.applyPlugins({ - key: "beforeRender", - type: ApplyPluginsType.modify, - initialValue: { - loading: null, - action: null - }, - }); - if (typeof beforeRenderConfig.action === "function") { - const rootElement = document.createElement('div'); - document.body.appendChild(rootElement) - const app = createApp(beforeRenderConfig.loading); - app.mount(rootElement); - try { - const initialState = await beforeRenderConfig.action({router, history}); - updateInitialState(initialState || {}) - } catch(e){ - console.error(`[fes] beforeRender执行出现异常:`); - console.error(e); - } - app.unmount(); - app._container.innerHTML = ''; - document.body.removeChild(rootElement); - } + if(isInit){ + return next() } - // 避免在beforeRender action中操作路由或者location.href,构建后会出现卡死 - setTimeout(() => { - next(); - }, 0); + isInit = true + const beforeRenderConfig = plugin.applyPlugins({ + key: "beforeRender", + type: ApplyPluginsType.modify, + initialValue: { + loading: null, + action: null + }, + }); + if (typeof beforeRenderConfig.action !== "function") { + return next(); + } + const rootElement = document.createElement('div'); + document.body.appendChild(rootElement) + const app = createApp(beforeRenderConfig.loading); + app.mount(rootElement); + try { + const initialState = await beforeRenderConfig.action({router, history}); + updateInitialState(initialState || {}) + next(); + } catch(e){ + next(false); + console.error(`[fes] beforeRender执行出现异常:`); + console.error(e); + } + app.unmount(); + app._container.innerHTML = ''; + document.body.removeChild(rootElement); }) plugin.applyPlugins({