main.ts
3.92 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import type { QiankunProps } from "vite-plugin-qiankun/dist/helper";
import type { App as VueApp } from "vue";
import { createApp } from "vue";
import CommonUI from "@venus/common-ui-vue";
import ElementPlus from "element-plus";
import zhCn from "element-plus/es/locale/lang/zh-cn";
// import './styles/element/index.scss';
import { createPinia } from "pinia";
import {
qiankunWindow,
renderWithQiankun,
} from "vite-plugin-qiankun/dist/helper";
import App from "./App.vue";
import { createRouter } from "./router";
import "element-plus/dist/index.css";
import "./assets/style.css";
let app: null | VueApp<Element> = null;
let router: any = null;
/**
* 渲染函数
* @param props qiankun 传递的参数
*/
async function render(props: QiankunProps = {}) {
const { container } = props;
app = createApp(App);
// 注册通用 UI 组件库
app.use(CommonUI);
// 配置 Pinia
app.use(createPinia());
// 配置 Element-Plus
app.use(ElementPlus, {
// 改中文
locale: zhCn
});
// 配置路由 - 根据是否在 qiankun 环境中使用不同的 base
const routerBase = qiankunWindow.__POWERED_BY_QIANKUN__
? props.routerBase || "/assisant/"
: "/";
router = createRouter(routerBase);
app.use(router);
// 初始化通信管理器(仅在 qiankun 环境中)
if (qiankunWindow.__POWERED_BY_QIANKUN__ && props.onGlobalStateChange) {
const { subAppCommunication } = await import("./utils/communication");
subAppCommunication.init(props);
// 将通信管理器挂载到全局属性,方便组件使用
app.config.globalProperties.$communication = subAppCommunication;
}
// 初始化 GSC Request(仅在 qiankun 环境中)
if (qiankunWindow.__POWERED_BY_QIANKUN__ && props.getGscRequest) {
try {
const { gscRequestManager } = await import("./utils/gscRequest");
await gscRequestManager.init(props);
// 将 GSC Request 管理器挂载到全局属性,方便组件使用
app.config.globalProperties.$gscRequest = gscRequestManager;
// console.log('[子应用] GSC Request 管理器已初始化并挂载');
} catch (error) {
console.error("[子应用] GSC Request 管理器初始化失败:", error);
}
}
// 挂载应用
const containerEl = container
? container.querySelector("#app")
: document.querySelector("#app");
if (containerEl) {
app.mount(containerEl as Element);
} else {
console.error("[Vue3 微应用] 挂载容器未找到");
}
}
/**
* 独立运行时
* 直接访问子应用时,不是通过基座加载
*/
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
render();
}
/**
* qiankun 生命周期钩子 - bootstrap
* 只会在微应用初始化的时候调用一次,下次微应用重新进入时会直接调用 mount 钩子
*/
export async function bootstrap() {
// console.log('[Vue3 微应用钩子] bootstrap');
}
/**
* qiankun 生命周期钩子 - mount
* 应用每次进入都会调用 mount 方法,通常在这里触发应用的渲染方法
*/
export async function mount(props: QiankunProps) {
// console.log('Vue3 微应用钩子 mount', props);
render(props);
}
/**
* qiankun 生命周期钩子 - unmount
* 应用每次切出/卸载都会调用 unmount 方法
*/
export async function unmount(props: QiankunProps) {
console.log("[子应用] unmount", props);
// 销毁通信管理器
if (qiankunWindow.__POWERED_BY_QIANKUN__) {
const { subAppCommunication } = await import("./utils/communication");
subAppCommunication.destroy();
// 销毁 GSC Request 管理器
const { gscRequestManager } = await import("./utils/gscRequest");
gscRequestManager.destroy();
}
if (app) {
app.unmount();
app = null;
}
if (router) {
router = null;
}
}
/**
* qiankun 生命周期钩子 - update (可选)
* 主应用向子应用下发新的props时触发
*/
export async function update(props: QiankunProps) {
console.log("[Vue3 微应用] update", props);
}
// 使用 vite-plugin-qiankun 提供的渲染函数
renderWithQiankun({
bootstrap,
mount,
unmount,
update,
});