fix(plugin-qiankun): beforeRender中rootElement未正确获取子应用的rootElement

This commit is contained in:
harrywan 2021-05-18 21:35:16 +08:00
parent 248a7f693a
commit 55f34ad746
4 changed files with 39 additions and 33 deletions

View File

@ -1,5 +1,7 @@
import { access as accessApi } from '@fesjs/fes'; import { access as accessApi } from '@fesjs/fes';
import PageLoading from '@/components/PageLoading'; import PageLoading from '@/components/PageLoading';
import Antdv from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';
export const beforeRender = { export const beforeRender = {
loading: <PageLoading />, loading: <PageLoading />,
@ -13,3 +15,7 @@ export const beforeRender = {
}); });
} }
}; };
export const onAppCreated = ({ app }) => {
app.use(Antdv);
};

View File

@ -1,8 +1,12 @@
<template> <template>
<div> <div>
main main
<a-tabs v-model:activeKey="activeKey">
<a-tab-pane key="1" tab="Tab 1"> <MicroAppWithMemoHistory key="1" name="app1" url="/app1" /></a-tab-pane>
<a-tab-pane key="2" tab="Tab 2"><MicroAppWithMemoHistory key="2" name="app1" url="/app1/test" /></a-tab-pane>
<a-tab-pane key="3" tab="Tab 3">Content of Tab Pane 3</a-tab-pane>
</a-tabs>
</div> </div>
<MicroAppWithMemoHistory name="app1" :url="url" />
</template> </template>
<config> <config>
{ {
@ -24,6 +28,7 @@ export default {
}, 3000); }, 3000);
}); });
return { return {
activeKey: ref('1'),
url url
}; };
} }

View File

@ -165,7 +165,7 @@ export default function (api) {
api.addEntryCode( api.addEntryCode(
() => ` () => `
export const bootstrap = qiankun_genBootstrap(completeClientRender, app); export const bootstrap = qiankun_genBootstrap(clientRender, app);
export const mount = qiankun_genMount('#${api.config.mountElementId}'); export const mount = qiankun_genMount('#${api.config.mountElementId}');
export const unmount = qiankun_genUnmount(); export const unmount = qiankun_genUnmount();
export const update = qiankun_genUpdate(); export const update = qiankun_genUpdate();

View File

@ -40,29 +40,7 @@ const renderClient = (opts = {}) => {
return app; return app;
} }
const getClientRender = (args = {}) => plugin.applyPlugins({ const beforeRender = async ({rootElement}) => {
key: 'render',
type: ApplyPluginsType.compose,
initialValue: () => {
const opts = plugin.applyPlugins({
key: 'modifyClientRenderOpts',
type: ApplyPluginsType.modify,
initialValue: {
initialState: args.initialState,
routes: args.routes || getRoutes(),
plugin,
rootElement: '{{{ rootElement }}}',
{{#enableTitle}}
defaultTitle: `{{{ defaultTitle }}}`,
{{/enableTitle}}
},
});
return renderClient(opts);
},
args,
});
const beforeRender = async () => {
const beforeRenderConfig = plugin.applyPlugins({ const beforeRenderConfig = plugin.applyPlugins({
key: "beforeRender", key: "beforeRender",
type: ApplyPluginsType.modify, type: ApplyPluginsType.modify,
@ -74,7 +52,7 @@ const beforeRender = async () => {
let initialState = {}; let initialState = {};
if (typeof beforeRenderConfig.action === "function") { if (typeof beforeRenderConfig.action === "function") {
const app = createApp(beforeRenderConfig.loading); const app = createApp(beforeRenderConfig.loading);
app.mount('{{{ rootElement }}}'); app.mount(rootElement);
try { try {
initialState = await beforeRenderConfig.action(); initialState = await beforeRenderConfig.action();
} catch(e){ } catch(e){
@ -86,14 +64,31 @@ const beforeRender = async () => {
return initialState; return initialState;
}; };
const completeClientRender = async () => { const getClientRender = (args = {}) => plugin.applyPlugins({
const initialState = await beforeRender(); key: 'render',
const clientRender = getClientRender({initialState}); type: ApplyPluginsType.compose,
const app = clientRender(); initialValue: async () => {
return app; const opts = plugin.applyPlugins({
}; key: 'modifyClientRenderOpts',
type: ApplyPluginsType.modify,
initialValue: {
routes: args.routes || getRoutes(),
plugin,
rootElement: '{{{ rootElement }}}',
{{#enableTitle}}
defaultTitle: `{{{ defaultTitle }}}`,
{{/enableTitle}}
},
});
const initialState = await beforeRender(opts);
return renderClient({...opts, initialState});
},
args,
});
const app = completeClientRender(); const clientRender = getClientRender();
const app = clientRender();
{{{ entryCode }}} {{{ entryCode }}}