main.ts 2.25 KB
/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2025-11-11 15:39:04
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2025-11-11 19:17:19
 * @FilePath: \venus\apps\frontend\micro-vue\scrm-app-vue\src\main.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createApp } from 'vue'
import App from './App.vue'

let app: ReturnType<typeof createApp> | null = null
let mountEl: Element | null = null

function ensureContainerEl(container?: Element | null) {
  if (!container) return document.getElementById('app')
  const id = 'app'
  let el = container.querySelector('#' + id)
  if (!el) {
    el = document.createElement('div')
    el.id = id
    container.appendChild(el)
  }
  return el
}

function render(props: any = {}) {
  const { container } = props
  mountEl = ensureContainerEl(container)
  if (!mountEl) throw new Error('Micro app mount element not found')
  // 若已存在旧实例(可能由 HMR 或重复挂载造成),先安全卸载并清空容器
  if (app) {
    try {
      app.unmount()
    } catch {}
    ;(mountEl as HTMLElement).innerHTML = ''
    app = null
  }
  app = createApp(App)
  app.mount(mountEl)
}

export async function bootstrap() {
  // 可在此进行应用初始化逻辑
  console.log("bootstrap");
  
}

export async function mount(props: any) {
    console.log("mount",props);
  
    return new Promise((resolve, reject) => {
    // Always reject with an Error.
    reject(new Error('hi'));
  });
  // render(props)
}

export async function unmount(_props: any) {
  try {
    app?.unmount()
  } finally {
    if (mountEl) {
      // 清空容器,确保二次挂载不会触发 HMR 卸载异常
      ;(mountEl as HTMLElement).innerHTML = ''
    }
    app = null
    mountEl = null
  }
}

// 独立运行(本地开发直接访问)
if (!(window as any).__POWERED_BY_QIANKUN__) {
  render()
}