index.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. debugger
  26. if (app) {
  27. const instance = activeApps[app['activeRule']]
  28. if (instance) {
  29. instance.update()
  30. } else {
  31. if(toPath){
  32. app['props']['publicPath'] = toPath
  33. }
  34. activeApps[app['activeRule']] = loadMicroApp(app) // 手动加载子应用
  35. }
  36. }
  37. }
  38. // 卸载app的方法
  39. const unmountMicroApps = async multipleApp => {
  40. for (const key in activeApps) {
  41. const isExist = multipleApp.some(
  42. name => name == key
  43. )
  44. if (isExist) {
  45. activeApps[key].unmount()
  46. activeApps[key] = null
  47. delete activeApps[key]
  48. }
  49. }
  50. }
  51. export { mountMicroApp, unmountMicroApps }