微前端集成
本章节介绍如何将插件前端集成到 Fastdotnet 主应用的 qiankun 微前端架构中。
📖 本章内容
- qiankun 基础概念
- 微应用配置
- 生命周期钩子
- 样式隔离
- JS 沙箱
- 通信机制
- 常见问题
🔌 快速开始
1. 配置微前端入口
确保 micro-index.html 正确配置:
html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>MyPlugin Admin</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>2. 导出生命周期
在 src/main.ts 中导出 qiankun 生命周期:
typescript
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
let app: any = null
// 渲染函数
function render(props: any = {}) {
const { container } = props
app = createApp(App)
app.use(router)
app.mount(container ? container.querySelector('#app') : '#app')
}
// qiankun 生命周期
export async function bootstrap() {
console.log('MyPlugin Admin bootstrapped')
}
export async function mount(props: any) {
console.log('MyPlugin Admin mounted', props)
render(props)
}
export async function unmount() {
app?.unmount()
app = null
console.log('MyPlugin Admin unmounted')
}
// 独立运行时
if (!window.__POWERED_BY_QIANKUN__) {
render()
}📚 详细内容
💡 提示: 本章节正在完善中,详细内容即将添加。
核心主题
- qiankun 原理 - 微前端架构
- 主应用配置 - 注册微应用
- 子应用开发 - 生命周期管理
- 路由同步 - 主子应用路由协调
- 状态共享 - GlobalState 使用
- 样式隔离 - Shadow DOM / Scoped CSS
- 性能优化 - 预加载和缓存
- 调试技巧 - 开发和生产环境