MicroAppContainer.vue 2.81 KB
<script setup lang="ts">
import { onMounted, onUnmounted, ref } from "vue";
import { useRoute } from "vue-router";

import { getMicroAppByPath } from "../apps";

const route = useRoute();
const loading = ref(true);
const error = ref("");

let timer: null | number = null;

/**
 * 初始化微应用容器
 */
onMounted(async () => {
	// 获取当前路径对应的微应用(等待配置加载完成)
	const microApp = await getMicroAppByPath(route.path, true);

	if (!microApp) {
		error.value = "未找到对应的微应用配置";
		loading.value = false;
		return;
	}

	// 模拟加载延迟和错误处理
	timer = window.setTimeout(() => {
		loading.value = false;

		// 这里可以添加更多的加载逻辑
		console.log("[MicroAppContainer] 微应用容器已准备就绪:", microApp.name);
	}, 1000);
});

/**
 * 清理定时器
 */
onUnmounted(() => {
	if (timer) {
		clearTimeout(timer);
		timer = null;
	}
});

/**
 * 重试加载
 */
const handleRetry = () => {
	error.value = "";
	loading.value = true;

	timer = window.setTimeout(() => {
		loading.value = false;
	}, 1000);
};
</script>

<template>
    <div class="micro-app-container">
        <!-- 微应用加载状态显示 -->
        <!-- <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <p class="loading-text">正在加载微应用...</p>
    </div>
     -->
        <!-- 微应用加载失败提示 -->
        <div v-if="error" class="error-container">
            <div class="error-icon">
                ⚠️
            </div>
            <p class="error-text">
                {{ error }}
            </p>
            <button class="retry-button" @click="handleRetry">
                重试
            </button>
        </div>
    </div>
</template>

<style scoped>
@keyframes spin {
	0% {
		transform: rotate(0deg);
	}

	100% {
		transform: rotate(360deg);
	}
}

.micro-app-container {
	width: 100%;
}

/* 加载状态 */
.loading-container {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}

.loading-spinner {
	width: 48px;
	border: 4px solid #f3f3f3;
	border-top: 4px solid #1890ff;
	border-radius: 50%;
	animation: spin 1s linear infinite;
}

.loading-text {
	margin-top: 16px;
	font-size: 14px;
	color: #666;
}

/* 错误状态 */
.error-container {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	min-height: 400px;
}

.error-icon {
	margin-bottom: 16px;
	font-size: 48px;
}

.error-text {
	margin-bottom: 16px;
	font-size: 14px;
	color: #ff4d4f;
}

.retry-button {
	padding: 8px 24px;
	font-size: 14px;
	color: white;
	cursor: pointer;
	background-color: #1890ff;
	border: none;
	border-radius: 4px;
	transition: background-color 0.3s;
}

.retry-button:hover {
	background-color: #40a9ff;
}

.retry-button:active {
	background-color: #096dd9;
}
</style>