8 changed files with 74 additions and 63 deletions
@ -1,68 +1,61 @@ |
|||||||
/** |
/** |
||||||
* qiankun配置 |
* @program: kicc-ui |
||||||
|
* @description: 微应用乾坤配置 |
||||||
|
* @author: wangxiang4 |
||||||
|
* @create: 2023/7/20 |
||||||
*/ |
*/ |
||||||
|
|
||||||
import { registerMicroApps, start, runAfterFirstMounted, addGlobalUncaughtErrorHandler } from 'qiankun'; |
import { registerMicroApps, start, runAfterFirstMounted, addGlobalUncaughtErrorHandler } from 'qiankun'; |
||||||
import { apps } from './apps'; |
import { apps } from './apps'; |
||||||
import { getProps, initGlState } from './state'; |
import { getSubDefineProps, initGlState } from './state'; |
||||||
|
|
||||||
/** |
|
||||||
* 重构apps |
|
||||||
*/ |
|
||||||
function filterApps() { |
|
||||||
apps.forEach((item) => { |
|
||||||
//主应用需要传递给微应用的数据。
|
|
||||||
item.props = getProps(); |
|
||||||
//微应用触发的路由规则
|
|
||||||
item.activeRule = genActiveRule('/' + item.activeRule); |
|
||||||
}); |
|
||||||
return apps; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
/** 匹配激活微服务路由规则 */ |
||||||
* 路由监听 |
function listenerRouteActiveRule(routerPath) { |
||||||
* @param {*} routerPrefix 前缀 |
console.info(location.pathname,routerPath,location.pathname.startsWith(routerPath)); |
||||||
*/ |
return (location) => location.pathname.endsWith(routerPath); |
||||||
function genActiveRule(routerPrefix) { |
|
||||||
return (location) => location.pathname.startsWith(routerPrefix); |
|
||||||
} |
} |
||||||
|
|
||||||
/** |
/** 微应用注册 */ |
||||||
* 微应用注册 |
|
||||||
*/ |
|
||||||
function registerApps() { |
function registerApps() { |
||||||
const _apps = filterApps(); |
const microApps = apps.map((item) => { |
||||||
registerMicroApps(_apps, { |
// 主应用传递给微应用的数据
|
||||||
|
item.props = getSubDefineProps(); |
||||||
|
// 微应用触发的路由规则
|
||||||
|
item.activeRule = listenerRouteActiveRule('/' + item.activeRule); |
||||||
|
return item; |
||||||
|
}); |
||||||
|
registerMicroApps(microApps, { |
||||||
beforeLoad: [ |
beforeLoad: [ |
||||||
(loadApp) => Promise.resolve(()=>{ |
(loadApp) => Promise.resolve(()=>{ |
||||||
console.log('before load', loadApp); |
console.info('before load', loadApp); |
||||||
}), |
}), |
||||||
], |
], |
||||||
beforeMount: [ |
beforeMount: [ |
||||||
(mountApp) => Promise.resolve(()=>{ |
(mountApp) => Promise.resolve(()=>{ |
||||||
console.log('before mount', mountApp); |
console.info('before mount', mountApp); |
||||||
}), |
}), |
||||||
], |
], |
||||||
afterMount: [ |
afterMount: [ |
||||||
(mountApp) => Promise.resolve(()=>{ |
(mountApp) => Promise.resolve(()=>{ |
||||||
console.log('before mount', mountApp); |
console.info('before mount', mountApp); |
||||||
}), |
}), |
||||||
], |
], |
||||||
afterUnmount: [ |
afterUnmount: [ |
||||||
(unloadApp) => Promise.resolve(()=>{ |
(unloadApp) => Promise.resolve(()=>{ |
||||||
console.log('after unload', unloadApp); |
console.info('after unload', unloadApp); |
||||||
}), |
}), |
||||||
], |
], |
||||||
}); |
}); |
||||||
// 设置默认子应用,与 genActiveRule中的参数保持一致
|
// 首个微应用加载之后的回调函数用于开启一些监控或者埋点脚本进行数据分析
|
||||||
// setDefaultMountApp();
|
|
||||||
// 第一个微应用 mount 后需要调用的方法,比如开启一些监控或者埋点脚本。
|
|
||||||
runAfterFirstMounted(() => console.log('开启监控')); |
runAfterFirstMounted(() => console.log('开启监控')); |
||||||
// 添加全局的未捕获异常处理器。
|
// 全局的未捕获异常处理器
|
||||||
addGlobalUncaughtErrorHandler((event) => console.log(event)); |
addGlobalUncaughtErrorHandler((event) => console.log(event)); |
||||||
// 定义全局状态
|
// 初始化全局通信
|
||||||
initGlState(); |
initGlState(); |
||||||
//启动qiankun
|
// 启动qiankun
|
||||||
start({}); |
start({ |
||||||
|
sandbox: { experimentalStyleIsolation: true } |
||||||
|
}); |
||||||
} |
} |
||||||
|
|
||||||
export default registerApps; |
export default registerApps; |
||||||
|
@ -1,39 +1,37 @@ |
|||||||
/** |
/** |
||||||
*公共数据 |
* @program: kicc-ui |
||||||
|
* @description: 微应用数据通信 |
||||||
|
* @author: wangxiang4 |
||||||
|
* @create: 2023/7/20 |
||||||
*/ |
*/ |
||||||
|
|
||||||
import { initGlobalState } from 'qiankun'; |
import { initGlobalState } from 'qiankun'; |
||||||
import { store } from '/@/store'; |
import { store } from '/@/store'; |
||||||
import { router } from '/@/router'; |
import { router } from '/@/router'; |
||||||
import { getAccessToken } from '/@/utils/auth'; |
import { getAccessToken } from '/@/utils/auth'; |
||||||
|
|
||||||
//定义传入子应用的数据
|
/** 子应用的数据 */ |
||||||
export function getProps(){ |
export function getSubDefineProps() { |
||||||
return { |
return { |
||||||
data: { |
data: { |
||||||
publicPath: '/', |
publicPath: '/', |
||||||
token: getAccessToken(), |
token: getAccessToken(), |
||||||
store, |
store, |
||||||
router, |
router |
||||||
}, |
} |
||||||
}; |
}; |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* 定义全局状态,并返回通信方法,在主应用使用,微应用通过 props 获取通信方法。 |
* 初始化全局通信状态 |
||||||
* @param state 主应用穿的公共数据 |
* @param state 主应用穿的公共数据 |
||||||
*/ |
*/ |
||||||
export function initGlState(info = { userName: 'admin' }) { |
export function initGlState(info = { test: 'demo' }) { |
||||||
// 初始化state
|
|
||||||
const actions = initGlobalState(info); |
const actions = initGlobalState(info); |
||||||
// 设置新的值
|
// todo: 可根据自身的数据传输需求进行更改
|
||||||
actions.setGlobalState(info); |
actions.setGlobalState(info); |
||||||
// 注册 观察者 函数 - 响应 globalState 变化,在 globalState 发生改变时触发该 观察者 函数。
|
|
||||||
actions.onGlobalStateChange((newState, prev) => { |
actions.onGlobalStateChange((newState, prev) => { |
||||||
// state: 变更后的状态; prev 变更前的状态
|
console.info(`newState:${newState}`, `prev:${prev}`); |
||||||
console.info('newState', newState); |
for (const key in newState) console.info('newState:value', key); |
||||||
console.info('prev', prev); |
|
||||||
for (const key in newState) { |
|
||||||
console.info('onGlobalStateChange', key); |
|
||||||
} |
|
||||||
}); |
}); |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue