当前位置: 首页 > news >正文

微前端架构常见框架

1. iframe

这里指的是每个微应用独立开发部署,通过 iframe 的方式将这些应用嵌入到父应用系统中,几乎所有微前端的框架最开始都考虑过 iframe,但最后都放弃,或者使用部分功能,原因主要有:

  • url 不同步。浏览器刷新 iframe url 状态丢失、后退前进按钮无法使用。

  • UI 不同步,DOM 结构不共享。想象一下屏幕右下角 1/4 的 frame 里来一个带遮罩层的弹框,同时我们要求这个弹框要浏览器居中显示,还要浏览器重置大小时自动居中。

  • 全局上下文完全隔离,内存变量不共享。iframe 内外系统的通信、数据同步等需求,主应用的 cookie 要透传到根域名都不同的子应用中实现免登效果。

  • 慢。每次子应用进入都是一次浏览器上下文重建、资源重新加载的过程。

2. single-spa

single-spa 是一个基础的微前端框架,通俗点说,提供了生命周期的概念,并负责调度子应用的生命周期 挟持 url 变化事件和函数,url 变化时匹配对应子应用,并执行生命周期流程,完整的生命周期流程为:

2.1. Root Config

index.html:静态资源、子应用入口声明。

// index.html
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>Polyglot Microfrontends</title><meta name="importmap-type" content="systemjs-importmap" /><script type="systemjs-importmap" src="https://storage.googleapis.com/polyglot_microfrontends.app/importmap.json"></script><!-- if (isLocal) { --><script type="systemjs-importmap">{"imports": {"@polyglot-mf/root-config": "/localhost:9000/polyglot-mf-root-config.js"}}</script><!-- } --><script src="https://cdn.jsdelivr.net/npm/import-map-overrides@2.2.0/dist/import-map-overrides.js"></script><script src="https://cdn.jsdelivr.net/npm/systemjs@6.8.3/dist/system.min.js"></script><script src="https://cdn.jsdelivr.net/npm/systemjs@6.8.3/dist/extras/amd.min.js"></script>
</head>
<body><script>System.import('@polyglot-mf/root-config');System.import('@polyglot-mf/styleguide');</script><import-map-overrides-full show-when-local-storage="devtools" dev-libs></import-map-overrides-full>
</body>
</html>

main.js:子应用注册及启动。

// main.jsimport { registerApplication, start } from "single-spa";registerApplication({name: "@polyglot-mf/navbar",app: () => System.import("@polyglot-mf/navbar"),activeWhen: "/",
});registerApplication({name: "@polyglot-mf/clients",app: () => System.import("@polyglot-mf/clients"),activeWhen: "/clients",
});registerApplication({name: "@polyglot-mf/account-settings",app: () => loadWithoutAMD("@polyglot-mf/account-settings"),activeWhen: "/settings",
});start();// A lot of angularjs libs are compiled to UMD, and if you don't process them with webpack
// the UMD calls to window.define() can be problematic.
function loadWithoutAMD(name) {return Promise.resolve().then(() => {let globalDefine = window.define;delete window.define;return System.import(name).then((module) => {window.define = globalDefine;return module;});});
}

single-spa提倡在浏览器中直接使用微前端应用,而不是通过构建工具进行打包。在single-spa中,参考的是SystemJS的思路,从而支持在浏览器中使用import、export。其实通过构建工具也可以实现类似的功能,webpack 可以将 ES6 的模块语法转换为浏览器可以理解的格式,并进行打包和优化。

2.2. single-spa-layout

指定single-spa在index.html中哪里渲染指定的子应用,constructApplications,constructRoutes及constructLayoutEngine 是针对定义的layout中的元素获取属性,再批量注册。

<html><head><template id="single-spa-layout"><single-spa-router><nav class="topnav"><application name="@organization/nav"></application></nav><div class="main-content"><route path="settings"><application name="@organization/settings"></application></route><route path="clients"><application name="@organization/clients"></application></route></div><footer><application name="@organization/footer"></application></footer></single-spa-router></template><script>// 注册import { registerApplication, start } from 'single-spa';import {constructApplications,constructRoutes,constructLayoutEngine} from 'single-spa-layout';// 获取routesconst routes = constructRoutes(document.querySelector("#single-spa-layout"));// 获取所有的子应用const applications = constructApplications({routes,loadApp({ name }) {return System.import(name); // SystemJS 引入入口JS},});// 生成LayoutEngineconst layoutEngine = constructLayoutEngine(routes, applications);// 批量注册子应用applications.forEach(registerApplication);// 启动主应用start();</script></head></html>

2.3. 子应用注册

single-spa针对子应用不同类型的子应用(如Vue、React等)都进行封装,但核心还是bootstrap、mount、unmount生命周期钩子。

import SubApp from './index.tsx'export const bootstrap = () => {}export const mount = () => {// 使用 React 来渲染子应用的根组件ReactDOM.render(<SubApp />, document.getElementById('root'));
}export const unmount = () => {}

2.4. 样式隔离

提供子应用CSS的引入和移除:single-spa-css

// 代码块
import singleSpaCss from 'single-spa-css';const cssLifecycles = singleSpaCss({// 这里放你导出的 CSS,如果 webpackExtractedCss 为 true,可以不指定cssUrls: ["https://example.com/main.css"],// 是否要使用从 Webpack 导出的 CSS,默认为 falsewebpackExtractedCss: false,// 是否 unmount 后被移除,默认为 trueshouldUnmount: true,// 超时,不废话了,都懂的timeout: 5000
})const reactLifecycles = singleSpaReact({...})// 加入到子应用的 bootstrap 里
export const bootstrap = [cssLifecycles.bootstrap,reactLifecycles.bootstrap
]export const mount = [// 加入到子应用的 mount 里,一定要在前面,不然 mount 后会有样式闪一下的问题cssLifecycles.mount,reactLifecycles.mount
]export const unmount = [// 和 mount 同理reactLifecycles.unmount,cssLifecycles.unmount
]

子应用间CSS样式隔离,推荐使用scoped CSS和shadowDOM.

2.5. JS隔离

给每个子应用添加全局变量,加入时添加,移除是去除:single-spa-leaked-globals.

// 代码块
import singleSpaLeakedGlobals from 'single-spa-leaked-globals';// 其它 single-spa-xxx 提供的生命周期函数
const frameworkLifecycles = ...const leakedGlobalsLifecycles = singleSpaLeakedGlobals({globalVariableNames: ['$', 'jQuery', '_'], // 新添加的全局变量
})export const bootstrap = [leakedGlobalsLifecycles.bootstrap, // 放在第一位frameworkLifecycles.bootstrap,
]export const mount = [leakedGlobalsLifecycles.mount, // mount 时添加全局变量,如果之前有记录在案的,直接恢复frameworkLifecycles.mount,
]export const unmount = [leakedGlobalsLifecycles.unmount, // 删掉新添加的全局变量frameworkLifecycles.unmount,
]
<
http://www.dtcms.com/a/347257.html

相关文章:

  • 深度学习之PyTorch框架(安装,手写数字识别)
  • pid自适应调节实战设计-基于输出电流的PI参数切换方案
  • deepseek应用技巧-MCP服务操作网页和文档
  • ESP32应用——UDP组播/广播(ESP-IDF框架)
  • AI 辅助编程完全新手指南 - 从零基础到高效协作
  • Java试题-选择题(15)
  • flink常见问题之超出文件描述符限制
  • frp v0.64.0 更新:开源内网穿透工具,最简洁教程
  • 生成一个256 点 Q15 正弦表,deepseek载了
  • TK 直播网络串流 通过VLC本地播放
  • 合合信息acge模型获C-MTEB第一,文本向量化迎来新突破
  • 数据库服务语句应用
  • Vmware虚拟机 处理器配置选项配置介绍
  • 香橙派板子Orange Pi 5 Max开启 WiFi 热点
  • Java 线程相关的三个常见接口、类
  • pytorch与torchvision版本对应情况
  • SHAP分析+KOA-RIME开普勒结合霜冰算法双重优化BP神经网络+9种映射方法+新数据预测!机器学习可解释分析!
  • Excel 条件高亮工具,秒高亮显示符合筛选条件的行数据
  • 数据结构与算法-字符串、数组和广义表(String Array List)
  • pixijs基础学习
  • Huggingface入门实践 图像处理CV与多模态模型调用(二)
  • Android 之wifi连接流程
  • 用 Go + GitHub Models API 打造一个免费的 ChatBot
  • 金山办公的服务端开发工程师-25届春招笔试编程题
  • 密码实现安全基础篇 . KAT(已知答案测试)技术解析与实践
  • 微服务的编程测评系统15-头像上传-OSS
  • Ceph OSD 硬盘重连导致设备名变化
  • 访问网络附加存储
  • `strcat` 字符串连接函数
  • 一文学会vue的动态权限控制