MicroHost.vue
2.69 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
<!--
* @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 17:12:31
* @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 18:14:05
* @FilePath: \venus\apps\frontend\micro-vue\scrm-base\src\pages\MicroHost.vue
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<script setup lang="ts">
import { computed, ref, onMounted, onBeforeUnmount } from 'vue'
import { useRoute } from 'vue-router'
import appsManifest from '../mfe/apps.json'
const route = useRoute()
const meta = computed(() => {
return appsManifest.apps.find((a) => route.path.startsWith(a.route))
})
const containerId = computed(() => {
const c = meta.value?.container || ''
return c.startsWith('#') ? c.slice(1) : c || `mfe-${(route.path.split('/').pop() || '').trim()}`
})
const loading = ref(true)
let observer: MutationObserver | null = null
function checkReady(el: Element | null) {
if (!el) return
const appRoot = el.querySelector('#app')
if (appRoot && appRoot.childNodes.length > 0) {
loading.value = false
observer?.disconnect()
observer = null
}
}
onMounted(() => {
const el = document.getElementById(containerId.value)
if (!el) return
// 初次检查
checkReady(el)
// 监听容器变化以检测子应用挂载完成
observer = new MutationObserver(() => checkReady(el))
observer.observe(el, { childList: true, subtree: true })
// 自动注册模式下,qiankun 将根据 activeRule 与容器自动挂载
})
onBeforeUnmount(() => {
observer?.disconnect()
observer = null
// 自动注册模式下无需手动卸载
})
</script>
<template>
<div style="padding: 12px;">
<div style="margin-bottom: 8px; font-size: 12px; color: #6b7280;">
<span>当前子应用:</span>
<strong>{{ meta?.displayName || '未知' }}</strong>
<span style="margin-left:8px;">入口:</span>
<code>{{ meta?.entry || '-' }}</code>
<span style="margin-left:8px;">容器:</span>
<code>#{{ containerId }}</code>
</div>
<div v-if="loading" class="mfe-loading">子应用加载中…</div>
<div :id="containerId"></div>
</div>
</template>
<style scoped>
.mfe-loading {
padding: 8px 12px;
margin-bottom: 8px;
font-size: 13px;
color: #374151;
background: #f9fafb;
border: 1px solid #e5e7eb;
border-radius: 4px;
}
</style>