feat(qiankun): 支持memoryHistory模式,嵌套子应用任意路由页面路由

This commit is contained in:
harrywan 2021-04-19 20:04:18 +08:00
parent 3737195362
commit ffc88ab656
3 changed files with 17 additions and 9 deletions

View File

@ -1,4 +1,4 @@
import { plugin, ApplyPluginsType, getRouter, getHistory } from '@@/core/coreExports'; import { plugin, ApplyPluginsType, getRouter, getHistory, destroyRouter } from '@@/core/coreExports';
{{#HAS_PLUGIN_MODEL}} {{#HAS_PLUGIN_MODEL}}
import { setModelState } from './qiankunModel'; import { setModelState } from './qiankunModel';
{{/HAS_PLUGIN_MODEL}} {{/HAS_PLUGIN_MODEL}}
@ -74,14 +74,10 @@ export function genMount(mountElementId) {
clientRenderOptsStack.push(clientRenderOpts); clientRenderOptsStack.push(clientRenderOpts);
if(props.url){ if(props.url){
history.url = props.url; history.url = props.url || '/';
} }
if(props.onRouterInit){ if(props.onRouterInit){
history.onRouterInit = props.onRouterInit; history.onRouterInit = props.onRouterInit;
const router = getRouter();
if(router){
history.onRouterInit(router);
}
} }
// 第一次 mount 会自动触发 render非第一次 mount 则需手动触发 // 第一次 mount 会自动触发 render非第一次 mount 则需手动触发
@ -118,6 +114,7 @@ export function genUnmount() {
const app = await cacheAppPromise; const app = await cacheAppPromise;
app.unmount(); app.unmount();
} }
destroyRouter();
const slaveRuntime = getSlaveRuntime(); const slaveRuntime = getSlaveRuntime();
if (slaveRuntime.unmount) { if (slaveRuntime.unmount) {
await slaveRuntime.unmount(props); await slaveRuntime.unmount(props);

View File

@ -293,7 +293,7 @@ export default function (api) {
api.addCoreExports(() => [ api.addCoreExports(() => [
{ {
specifiers: ['getRoutes', 'getRouter', 'getHistory'], specifiers: ['getRoutes', 'getRouter', 'getHistory', 'destroyRouter'],
source: absCoreFilePath source: absCoreFilePath
} }
]); ]);

View File

@ -40,9 +40,20 @@ export const createRouter = () => {
}; };
export const getRouter = ()=>{ export const getRouter = ()=>{
return router; if(!router){
console.warn(`[preset-build-in] router is null`)
}
return router;
} }
export const getHistory = ()=>{ export const getHistory = ()=>{
return history; if(!history){
console.warn(`[preset-build-in] history is null`)
}
return history;
}
export const destroyRouter = ()=>{
router = null;
history = null;
} }