dbsd_kczx/src/qiankun/state.ts

39 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-03-10 09:47:29 +08:00
/**
*
*/
2022-06-10 10:44:44 +08:00
import { initGlobalState } from 'qiankun';
import { store } from '/@/store';
import { router } from '/@/router';
2022-03-10 09:47:29 +08:00
import { getToken } from '/@/utils/auth';
//定义传入子应用的数据
export function getProps() {
2022-06-10 10:44:44 +08:00
return {
data: {
publicPath: '/',
token: getToken(),
store,
router,
},
};
2022-03-10 09:47:29 +08:00
}
/**
* ,使 props
* @param state 穿
*/
2022-06-10 10:44:44 +08:00
export function initGlState(info = { userName: 'admin' }) {
// 初始化state
const actions = initGlobalState(info);
// 设置新的值
actions.setGlobalState(info);
// 注册 观察者 函数 - 响应 globalState 变化,在 globalState 发生改变时触发该 观察者 函数。
actions.onGlobalStateChange((newState, prev) => {
// state: 变更后的状态; prev 变更前的状态
console.info('newState', newState);
console.info('prev', prev);
for (const key in newState) {
console.info('onGlobalStateChange', key);
}
});
2022-03-10 09:47:29 +08:00
}