main.ts 3.93 KB
import type { QiankunProps } from 'vite-plugin-qiankun/dist/helper';
import zhCn from "element-plus/es/locale/lang/zh-cn";
import type { App as VueApp } from 'vue';
import { createApp } from 'vue';
import ElementPlus from 'element-plus';
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);

  // 配置 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,
});