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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
| // 主应用入口 main.ts
import { registerMicroApps, start, initGlobalState } from 'qiankun'
// 1. 注册子应用
const apps = [
{
name: 'dashboard',
entry: '//localhost:7101',
container: '#subapp-container',
activeRule: '/dashboard',
props: {
routerBase: '/dashboard',
getGlobalState: () => store.getState()
}
},
{
name: 'settings',
entry: '//localhost:7102',
container: '#subapp-container',
activeRule: '/settings',
props: {
routerBase: '/settings'
}
},
{
name: 'billing',
entry: '//localhost:7103',
container: '#subapp-container',
activeRule: '/billing'
}
]
// 2. 配置全局状态
const initialState = {
user: null,
token: null,
theme: 'light'
}
const actions = initGlobalState(initialState)
// 3. 监听状态变化
actions.onGlobalStateChange((state, prev) => {
console.log('Global state changed:', state, prev)
})
// 4. 启动qiankun
registerMicroApps(apps, {
beforeLoad: [
app => {
console.log('Loading app:', app.name)
return Promise.resolve()
}
],
beforeMount: [
app => {
console.log('Mounting app:', app.name)
return Promise.resolve()
}
],
afterMount: [
app => {
console.log('Mounted app:', app.name)
return Promise.resolve()
}
],
beforeUnmount: [
app => {
console.log('Unmounting app:', app.name)
return Promise.resolve()
}
],
afterUnmount: [
app => {
console.log('Unmounted app:', app.name)
return Promise.resolve()
}
]
})
start({
sandbox: {
strictStyleIsolation: true, // 严格样式隔离
experimentalStyleIsolation: true // 实验性样式隔离
},
prefetch: true, // 预加载子应用资源
singular: false, // 是否单实例
fetch: window.fetch // 自定义fetch方法
})
|