123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- /**
- * 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) {
- 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 = async (multipleApp) => {
- 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 };
|