feat: 换更合理的方式

This commit is contained in:
wanchun 2023-03-29 16:35:42 +08:00
parent 9641b0dc67
commit 0eb8d88472

View File

@ -39,37 +39,37 @@ export const createRouter = (routes) => {
let isInit = false let isInit = false
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, from, next) => {
if(!isInit) { if(isInit){
isInit = true return next()
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);
}
} }
// 避免在beforeRender action中操作路由或者location.href构建后会出现卡死 isInit = true
setTimeout(() => { const beforeRenderConfig = plugin.applyPlugins({
next(); key: "beforeRender",
}, 0); 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({ plugin.applyPlugins({