index.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * qiankun配置
  3. */
  4. import { loadMicroApp } from 'qiankun';
  5. import { apps } from './apps';
  6. import { getProps } from './state';
  7. const activeApps = {};
  8. /**
  9. * 重构apps
  10. */
  11. function filterApps() {
  12. apps.forEach((item) => {
  13. //主应用需要传递给微应用的数据。
  14. item['props'] = getProps();
  15. console.log('主应用给子应用传的数据', item.props);
  16. //微应用触发的路由规则
  17. // @ts-ignore
  18. item.activeRule = item.activeRule.startsWith('/') ? item.activeRule : `/${item.activeRule}`;
  19. });
  20. return apps;
  21. }
  22. const mountMicroApp = (path, toPath?) => {
  23. const microApps = filterApps();
  24. const app = microApps.find((item) => path.startsWith(item['activeRule']));
  25. if (app) {
  26. const instance = activeApps[app['activeRule']];
  27. console.log('子应用实例--------------->', instance);
  28. if (instance) {
  29. instance.update();
  30. } else {
  31. if (toPath) {
  32. app['props']['data']['publicPath'] = toPath;
  33. }
  34. activeApps[app['activeRule']] = loadMicroApp(app, {
  35. fetch(url, ...args) {
  36. // 给指定的微应用 entry 开启跨域请求
  37. if (url === 'http://1.1.1.3:89/cookie/flash.js') {
  38. return window.fetch(url, {
  39. ...args,
  40. headers: {
  41. 'Access-Control-Allow-Origin': '*',
  42. },
  43. mode: 'cors',
  44. credentials: 'include',
  45. });
  46. }
  47. return window.fetch(url, ...args);
  48. },
  49. }); // 手动加载子应用
  50. }
  51. }
  52. };
  53. // 卸载app的方法
  54. const unmountMicroApps = async (multipleApp) => {
  55. for (const key in activeApps) {
  56. const isExist = multipleApp.some((name) => name == key);
  57. if (isExist) {
  58. activeApps[key].unmount();
  59. activeApps[key] = null;
  60. delete activeApps[key];
  61. }
  62. }
  63. };
  64. export { mountMicroApp, unmountMicroApps };