main.ts
2.25 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
/*
* @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()
}