| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- /**
- * 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']))
- debugger
- if (app) {
- const instance = activeApps[app['activeRule']]
- if (instance) {
- instance.update()
- } else {
- if(toPath){
- app['props']['publicPath'] = toPath
- }
- activeApps[app['activeRule']] = loadMicroApp(app) // 手动加载子应用
- }
- }
- }
- // 卸载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 }
|