lifecycles.ts
1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { FrameworkLifeCycles } from './types';
/**
* qiankun 微应用生命周期钩子配置
*/
export const microAppLifecycles: FrameworkLifeCycles<any> = {
/**
* 微应用加载前
*/
beforeLoad: async (app) => {
console.log('[qiankun] before load', app.name);
// 可以在这里做一些加载前的准备工作
// 例如:显示加载动画、设置全局状态等
},
/**
* 微应用挂载前
*/
beforeMount: async (app) => {
console.log('[qiankun] before mount', app.name);
// 确保主应用的数据已准备好并同步到全局状态
try {
const { communicationManager } = await import('./communication');
const globalState = communicationManager.getGlobalState();
// 如果用户信息为空,尝试从 Store 获取并同步
if (!globalState.user) {
const { useUserStore } = await import('@/stores');
const userStore = useUserStore();
if (userStore.userInfo) {
userStore.syncUserToQiankun();
}
}
} catch (error) {
console.warn('[qiankun] 同步用户信息失败:', error);
}
},
/**
* 微应用挂载后
*/
afterMount: async (app) => {
console.log('[qiankun] after mount', app.name);
// 可以在这里做一些挂载后的处理
// 例如:隐藏加载动画、记录日志、埋点统计等
},
/**
* 微应用卸载前
*/
beforeUnmount: async (app) => {
console.log('[qiankun] before unmount', app.name);
// 可以在这里做一些卸载前的清理工作
// 例如:保存状态、显示卸载动画等
},
/**
* 微应用卸载后
*/
afterUnmount: async (app) => {
console.log('[qiankun] after unmount', app.name);
// 可以在这里做一些卸载后的清理工作
// 例如:清除缓存、重置状态、移除事件监听等
},
};