Files
Yi.Admin/Yi.Ai.Vue3/src/main.ts

94 lines
2.8 KiB
TypeScript
Raw Normal View History

2025-06-17 22:37:37 +08:00
// 引入ElementPlus所有图标
import * as ElementPlusIconsVue from '@element-plus/icons-vue';
import { createApp } from 'vue';
import ElementPlusX from 'vue-element-plus-x';
2026-02-01 00:30:44 +08:00
import 'element-plus/dist/index.css';
2025-06-17 22:37:37 +08:00
import App from './App.vue';
2026-02-01 00:30:44 +08:00
import { logBuildInfo } from './config/version';
2025-06-17 22:37:37 +08:00
import router from './routers';
import store from './stores';
import './styles/index.scss';
import 'virtual:uno.css';
import 'virtual:svg-icons-register';
2025-07-20 21:01:41 +08:00
// 创建 Vue 应用
2025-06-17 22:37:37 +08:00
const app = createApp(App);
2025-07-20 21:01:41 +08:00
// 安装插件
2025-06-17 22:37:37 +08:00
app.use(router);
2026-02-01 00:30:44 +08:00
app.use(store);
2025-06-17 22:37:37 +08:00
app.use(ElementPlusX);
2025-07-20 21:01:41 +08:00
2026-02-01 00:30:44 +08:00
// 注册所有 Element Plus 图标(临时方案,后续迁移到 fontawesome
2025-06-17 22:37:37 +08:00
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
app.component(key, component);
}
2026-02-01 00:30:44 +08:00
// 输出构建信息(使用统一版本配置)
logBuildInfo();
2025-07-20 21:01:41 +08:00
2026-02-01 00:30:44 +08:00
import { nextTick } from 'vue';
2025-07-20 21:01:41 +08:00
// 挂载 Vue 应用
2025-06-17 22:37:37 +08:00
app.mount('#app');
2026-02-01 00:30:44 +08:00
/**
*
*
* 1. requestAnimationFrame
* 2.
* 3. CSS
* 4.
*/
function waitForPageRendered(): Promise<void> {
return new Promise((resolve) => {
const minDisplayTime = 800; // 最小展示时间 800ms避免闪烁
const maxWaitTime = 8000; // 最大等待时间 8 秒
const startTime = Date.now();
const checkRender = () => {
const elapsed = Date.now() - startTime;
const appElement = document.getElementById('app');
// 检查关键条件
const hasContent = appElement?.children.length > 0;
const hasVisibleHeight = (appElement?.offsetHeight || 0) > 200;
const hasRouterView = document.querySelector('.layout-container') !== null ||
document.querySelector('.el-container') !== null ||
document.querySelector('#app > div') !== null;
const isRendered = hasContent && hasVisibleHeight && hasRouterView;
const isMinTimeMet = elapsed >= minDisplayTime;
const isTimeout = elapsed >= maxWaitTime;
if ((isRendered && isMinTimeMet) || isTimeout) {
// 再多给一帧时间确保稳定
requestAnimationFrame(() => {
requestAnimationFrame(() => resolve());
});
} else {
requestAnimationFrame(checkRender);
}
};
// 等待 Vue 更新和浏览器绘制
nextTick(() => {
requestAnimationFrame(() => {
setTimeout(checkRender, 100);
});
});
});
}
// 第一阶段Vue 应用已挂载
if (typeof window.__hideAppLoader === 'function') {
window.__hideAppLoader('mounted');
}
// 等待页面真正渲染完成后再通知第二阶段
waitForPageRendered().then(() => {
if (typeof window.__hideAppLoader === 'function') {
window.__hideAppLoader('rendered');
}
});