/** * qiankun配置 */ import { loadMicroApp } from 'qiankun'; import { apps } from './apps'; import { getProps } from './state'; const activeApps = {}; /** * 重构apps */ function filterApps() { apps.forEach((item) => { //主应用需要传递给微应用的数据。 item['props'] = getProps(); console.log('主应用给子应用传的数据', item.props); //微应用触发的路由规则 // @ts-ignore item.activeRule = item.activeRule.startsWith('/') ? item.activeRule : `/${item.activeRule}`; }); return apps; } const mountMicroApp = (path, toPath?) => { const microApps = filterApps(); const app = microApps.find((item) => path.startsWith(item['activeRule'])); if (app) { if (activeApps[app['activeRule']]) { // return; activeApps[app['activeRule']].unmount(); activeApps[app['activeRule']] = null; delete activeApps[app['activeRule']]; } const instance = activeApps[app['activeRule']]; console.log('子应用实例--------------->', instance); if (instance) { instance.update(); } else { if (toPath) { app['props']['data']['publicPath'] = toPath; } activeApps[app['activeRule']] = loadMicroApp(app, { fetch(url, ...args) { // 给指定的微应用 entry 开启跨域请求 if (url === 'http://1.1.1.3:89/cookie/flash.js') { return window.fetch(url, { ...args, headers: { 'Access-Control-Allow-Origin': '*', }, mode: 'cors', credentials: 'include', }); } return window.fetch(url, ...args); }, }); // 手动加载子应用 } } }; // 卸载app的方法 const unmountMicroApps = (multipleApp) => { if (JSON.stringify(activeApps) !== '{}' && multipleApp.some) { for (const key in activeApps) { const isExist = multipleApp.some((name) => name == key); if (isExist) { activeApps[key].unmount(); activeApps[key] = null; delete activeApps[key]; } } } }; export { mountMicroApp, unmountMicroApps, activeApps };