l14107cb 发表于 2024-10-10 10:37:28

京东微前端框架MicroApp简介


    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/d6a91888489a40aea78a41e642a972d6~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728795097&amp;x-signature=Fv0cZbe5KdMm6skgmBnRjEaaQZE%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1、</span>MicroApp</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1.1 MicroApp简介</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">MicroApp<span style="color: black;">是由于</span>京东前端团队推出的一款微前端框架,它从组件化的思维,基于类WebComponent进行微前端的渲染,旨在降低上手难度、<span style="color: black;">提高</span>工作效率。MicroApp无关技术栈,<span style="color: black;">亦</span>不和业务绑定,<span style="color: black;">能够</span>用于任何前端框架。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">官网链接:</p>https://micro-zoe.github.io/micro-app/
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">源码<span style="color: black;">位置</span>:</p>https://github.com/micro-zoe/micro-app

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">微前端的概念<span style="color: black;">是由于</span>ThoughtWorks在2016年提出的,它借鉴了微服务的架构理念,核心在于将一个庞大的前端应用拆分成多个独立灵活的小型应用,<span style="color: black;">每一个</span>应用都<span style="color: black;">能够</span>独立<span style="color: black;">研发</span>、独立运行、独立<span style="color: black;">安排</span>,再将这些小型应用融合为一个完整的应用,<span style="color: black;">或</span>将<span style="color: black;">本来</span>运行已久、<span style="color: black;">无</span><span style="color: black;">相关</span>的几个应用融合为一个应用。微前端既<span style="color: black;">能够</span>将多个项目融合为一,又<span style="color: black;">能够</span>减少项目之间的耦合,<span style="color: black;">提高</span>项目扩展性,相比一整块的前端仓库,微前端架构下的前端仓库倾向于更小更灵活。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">微前端的<span style="color: black;">显现</span><span style="color: black;">重点</span>是为<span style="color: black;">认识</span>决了两个问题:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">1、随着项目迭代应用越来越庞大,难以<span style="color: black;">守护</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">2、跨团队或跨<span style="color: black;">分部</span>协作<span style="color: black;">研发</span>项目<span style="color: black;">引起</span>效率低下的问题。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/c87a8996e11a410aad8f36c7952d1cbe~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728795097&amp;x-signature=qLg34UFssAdRyZ9X9WYg7t%2BXM20%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1.2 关于MicroApp</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">MicroApp借鉴了WebComponent的思想,<span style="color: black;">经过</span>js沙箱、样式隔离、元素隔离、路由隔离模拟实现了ShadowDom的隔离特性,并结合CustomElement将微前端封装成一个类WebComponent组件,从而实现微前端的组件化渲染,旨在降低上手难度、<span style="color: black;">提高</span>工作效率。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">micro-app和技术栈无关,<span style="color: black;">亦</span>不和业务绑定,<span style="color: black;">能够</span>用于任何前端框架。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/181c86b3ce1d4da2b36c1d5fa3534f17~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728795097&amp;x-signature=rEJPSXbCVtSYm3vtEqNgsUQvqy4%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1.3 MicroApp优点</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1、<span style="color: black;">运用</span>简单</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">咱们</span>将所有功能都封装到一个类WebComponent组件中,从而实<span style="color: black;">此刻</span>基座应用中嵌入一行代码<span style="color: black;">就可</span>渲染一个微前端应用。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2、功能强大</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">micro-app<span style="color: black;">供给</span>了js沙箱、样式隔离、元素隔离、路由隔离、预加载、数据通信等一系列完善的功能。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">3、兼容所有框架</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">为了<span style="color: black;">保准</span>各个业务之间独立<span style="color: black;">研发</span>、独立<span style="color: black;">安排</span>的能力,micro-app做了<span style="color: black;">许多</span>兼容,在任何前端框架中都<span style="color: black;">能够</span>正常运行。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2、</span>快手上手</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在微前端架构中,微前端工程<span style="color: black;">重点</span>由主应用和子应用,下面<span style="color: black;">咱们</span>分别列出主应用和子应用需要进行的修改。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">2.1 主应用</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">首要</span>,<span style="color: black;">咱们</span>需要在主应用中安装依赖。</span></p>npm i @micro-zoe/micro-app --save<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">而后</span>,初始化micro-app。</span></p>// index.js
    import microApp from @micro-zoe/micro-app

    microApp.start()<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">而后</span>,在主应用中嵌入子应用,对应的React的写法如下。</span></p>export function MyPage () {
    return (
    &lt;div&gt;
    &lt;h1&gt;子应用&lt;/h1&gt;
    // name:应用名<span style="color: black;">叫作</span>, url:应用<span style="color: black;">位置</span>
    &lt;micro-app name=my-app url=http://localhost:3000/&gt;&lt;/micro-app&gt;
    &lt;/div&gt;
    )
    }<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Vue的写法如下。</span></p>&lt;template&gt;
    &lt;div&gt;
    &lt;h1&gt;子应用&lt;/h1&gt;
    &lt;!-- name:应用名<span style="color: black;">叫作</span>, url:应用<span style="color: black;">位置</span> --&gt;
    &lt;micro-app name=my-app url=http://localhost:3000/&gt;&lt;/micro-app&gt;
    &lt;/div&gt;
    &lt;/template&gt;<h1 style="color: black; text-align: left; margin-bottom: 10px;">2.2 子应用</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">对应的,<span style="color: black;">咱们</span>需要在子应用的webpack-dev-server的headers中设置跨域支持,如下所示。</span></p>devServer: {
    headers: {
    Access-Control-Allow-Origin: *,
    }
    },<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">完成以上<span style="color: black;">过程</span>即完成微前端的接入。<span style="color: black;">不外</span>,接入MicroApp需要<span style="color: black;">重视</span>以下几点:</p>1、name:必传参数,必须以字母开头,且不<span style="color: black;">能够</span>带特殊符号(中划线、下划线除外)
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2、url:必传参数,必须指向子应用的index.html,如:http://localhost:3000/ 或 </p>http://localhost:3000/index.html

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3、子应用必须支持跨域,跨域配置参考<span style="color: black;">这儿</span></span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3、</span>功能简介</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">3.1 JS沙箱</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在MicroApp开源后<span style="color: black;">咱们</span>收到社区的<span style="color: black;">有些</span>反馈,其中一条关于性能的问题<span style="color: black;">导致</span><span style="color: black;">咱们</span>的<span style="color: black;">注重</span>。这不是MicroApp才存在的问题,而是微前端<span style="color: black;">很久</span><span style="color: black;">败兴</span>的<span style="color: black;">困难</span>,在社区有<span style="color: black;">海量</span>关于沙箱性能问题的讨论,却始终<span style="color: black;">无</span>完美的<span style="color: black;">处理</span><span style="color: black;">方法</span>。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/b840e8dd2e75400a9983a378c0733ddb~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728795097&amp;x-signature=sOPJKMEMV6ChDDAfX7VOOECT3fo%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">MicroApp采用的是和qiankun<span style="color: black;">同样</span>的proxy+with的沙箱<span style="color: black;">方法</span>,这<span style="color: black;">亦</span>是目前js沙箱的主流<span style="color: black;">方法</span> 。with沙箱的功能非常完善,<span style="color: black;">然则</span>性能损耗却非常<span style="color: black;">显著</span>,在社区中<span style="color: black;">亦</span>不乏对with沙箱性能问题的讨论,但<span style="color: black;">始终</span><span style="color: black;">无</span><span style="color: black;">尤其</span>完美的<span style="color: black;">处理</span><span style="color: black;">方法</span>,这是<span style="color: black;">因为</span>with改变了js的<span style="color: black;">功效</span>域链,从而产生<span style="color: black;">海量</span>的重复请求。问题的根源是with,但又不全是with的问题,准确的说是with和proxy两者叠加造成的,with和proxy本质上的性能都不高,需要一种<span style="color: black;">方法</span>避免对这两个<span style="color: black;">办法</span>的频繁读取操作,MicroApp采用的<span style="color: black;">处理</span><span style="color: black;">方法</span>是变量前置和异步防抖。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">• 变量前置:<span style="color: black;">指的是</span><span style="color: black;">运用</span>Object.defineProperty定义全局变量,<span style="color: black;">经过</span>get和set设置响应数据,一是为了避免在proxy的get中进行多余的操作,二是defineProperty的性能比proxy更优秀 。<span style="color: black;">文案</span><span style="color: black;">源自</span><span style="color: black;">位置</span></p>https://www.yii666.com/blog/719852.html

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">• 异步防抖:<span style="color: black;">指的是</span>在子应用运行时对promise进行标记,<span style="color: black;">保证</span>在上一个promise执行完成之后才会进入下一个,避免并行触发,防止promise被频繁触发会<span style="color: black;">导致</span>性能损失。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/46f03e843ad94962afc244e3c2f884ab~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728795097&amp;x-signature=gNEdXM%2F7whcf7RcsjuQ49nrD7QQ%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这里</span><span style="color: black;">基本</span>上,MicroApp沙箱还<span style="color: black;">供给</span>了快照、缓存、预加载等功能,在<span style="color: black;">保准</span>功能不变的前提下,彻底<span style="color: black;">处理</span>沙箱的性能问题,升级后的沙箱运行效率媲美原生JS。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">不外</span>,<span style="color: black;">运用</span>MicroApp沙箱需要<span style="color: black;">重视</span>以下几点:</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1,子应用在沙箱环境中<span style="color: black;">怎样</span>获取到真实window</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">日前</span>有3种方式在子应用中获取<span style="color: black;">外边</span>真实window。</span></p><span style="color: black;">new Function("return window")() 或 Function("return window")()</span><span style="color: black;">(0, eval)(window)</span><span style="color: black;">window.rawWindow</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">2,子应用抛出错误信息:xxx 未定义</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">xxx未定义错误<span style="color: black;">包含</span>如下<span style="color: black;">有些</span>:</span></p><span style="color: black;">xxx is not defined</span><span style="color: black;">xxx is not a function</span><span style="color: black;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Cannot read properties of undefined</p>产生错误的<span style="color: black;">原由</span>是,在沙箱环境中,顶层变量不会泄漏为全局变量。例如在正常<span style="color: black;">状况</span>下,<span style="color: black;">经过</span> var name 或 function name () {} 定义的顶层变量会泄漏为全局变量,通过window.name或name就<span style="color: black;">能够</span>全局<span style="color: black;">拜访</span>。<span style="color: black;">然则</span>在沙箱环境下这些顶层变量<span style="color: black;">没法</span>泄漏为全局变量,window.name或name的值为undefined,<span style="color: black;">引起</span><span style="color: black;">显现</span>问题。
    </span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">处理</span>的方式有两种:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">第1</span>种:将 var name 或 function name () {} 修改为 window.name = xx</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">第二种:<span style="color: black;">经过</span>插件系统修改子应用代码,<span style="color: black;">例如</span><span style="color: black;">平常</span>的加载webpack打包的dll文件失败的问题,<span style="color: black;">由于</span>dll文件的内容和js<span style="color: black;">位置</span>相对固定,<span style="color: black;">能够</span>直接进行全局<span style="color: black;">查询</span>和修改。</span></p>microApp.start({
    plugins: {
    modules: {
    应用名<span style="color: black;">叫作</span>: [{
    loader(code, url) {
    if (url === xxx.js) {
    code = code.replace(var xx_dll=, window.xx_dll=)
    }
    return code
    }
    }]
    }
    }
    })<h1 style="color: black; text-align: left; margin-bottom: 10px;">3,基座<span style="color: black;">怎样</span>对子应用 document 的<span style="color: black;">有些</span>属性进行自定义代理扩展</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">微前端模式下,<span style="color: black;">一般</span>由基座负责设置站点标题,不<span style="color: black;">期盼</span>受到子应用的干扰。<span style="color: black;">然则</span><span style="color: black;">由于</span> microApp 对 documet 的代理处理过程,并<span style="color: black;">无</span>处理 document.title,<span style="color: black;">因此</span>子应用中可能<span style="color: black;">经过</span> document.title = xxx 意外改变了基座的站点标题。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">处理</span>的<span style="color: black;">办法</span>是,<span style="color: black;">经过</span> customProxyDocumentProps 对 document 的属性进行自定义代理扩展<span style="color: black;">经过</span>给title设置一个空函数,来忽略 document.title 执行。</span></p>microApp.start({
    customProxyDocumentProperties: new Map([
   
    ]),
    })<h1 style="color: black; text-align: left; margin-bottom: 10px;">3.2 虚拟路由系统</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">3.2.1 虚拟路由系统背景</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">微前端是将多个<span style="color: black;">区别</span>的web应用融合在<span style="color: black;">一块</span>渲染,但浏览器<span style="color: black;">仅有</span>一个路由系统,这很容易<span style="color: black;">导致</span>应用之间的路由冲突,最<span style="color: black;">平常</span>的<span style="color: black;">便是</span>vue3的路由冲突问题。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/b3255e06c38543b19f3c4936cbd8d2fe~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728795097&amp;x-signature=YmSM1pCak8rU2spJW2FI1WmvqTo%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">上面是vue-router作者<span style="color: black;">针对</span>在微前端环境下的冲突问题的回答,他认为vue-router<span style="color: black;">已然</span>覆盖足够多场景,微前端的问题应该由微前端<span style="color: black;">处理</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在<span style="color: black;">咱们</span>刚开源时,并<span style="color: black;">无</span>对路由进行隔离,用户<span style="color: black;">针对</span>MicroApp的问题几乎一半都和路由<span style="color: black;">关联</span>,<span style="color: black;">由于</span>一个路由系统<span style="color: black;">同期</span>满足多个应用的渲染容易<span style="color: black;">引起</span>冲突,<span style="color: black;">亦</span>非常反直觉和难以理解,于是<span style="color: black;">咱们</span>推出了虚拟路由系统。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/4f97afe6c8cc4638aefc87a684b3c0f6~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728795097&amp;x-signature=TDMi9uJFWSKfBYk8u%2BgyCQTcS1k%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">虚拟路由系统与浏览器的路由<span style="color: black;">行径</span>一致,它<span style="color: black;">经过</span>自定义location和history等核心路由API,重写了popState和hashChange事件,拦截路由导航和事件,并<span style="color: black;">供给</span>了一系列自定义API,模拟了在浏览器环境下的Web应用程序的渲染、<span style="color: black;">转</span>和返回等路由<span style="color: black;">行径</span>。子应用程序在这个虚拟路由系统中运行,与基座应用程序的路由相互隔离,从而避免相互影响,并<span style="color: black;">加强</span>了子应用程序与基座应用程序之间的交互能力。<span style="color: black;">经过</span>虚拟路由系统,基座应用程序<span style="color: black;">能够</span>方便地获取子应用程序的路由信息并<span style="color: black;">掌控</span>子应用程序的<span style="color: black;">转</span>,子应用程序的路由信息会<span style="color: black;">做为</span>参数同步到浏览器<span style="color: black;">位置</span>上。<span style="color: black;">另外</span>,虚拟路由系统还提供了许多功能,<span style="color: black;">帮忙</span><span style="color: black;">研发</span>人员<span style="color: black;">加强</span>工作效率。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">3.2.2 虚拟路由系统实战</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">路由模式</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">虚拟路由系统分为四种模式:search、native、native-scope、pure。search是默认模式,<span style="color: black;">一般</span>不需要特意设置,search模式下子应用的路由信息会<span style="color: black;">做为</span>query参数同步到浏览器<span style="color: black;">位置</span>上,如下。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/bfa8148efc184ce2879fa26a95d757e8~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728795097&amp;x-signature=CgbyP50XQ6I%2FY6UVkfxVOE1fQno%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>是设置单个子应用,如下所示。</span></p>&lt;micro-app name=xx url=xx router-mode=search&gt;&lt;/micro-app&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>需要全局设置,如下所示。</span></p>import microApp from @micro-zoe/micro-app
    microApp.start({
    router-mode: search,
    })<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">native模式下子应用完全基于浏览器路由系统进行渲染,比search模式<span style="color: black;">持有</span>更加简洁优雅的的浏览器<span style="color: black;">位置</span>,但相应的需要更加<span style="color: black;">繁杂</span>的路由配置。</p><span style="color: black;">倘若</span>是设置单个子应用,如下所示。
    &lt;micro-app name=xx url=xx router-mode=native&gt;&lt;/micro-app&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">全局设置参考如下:</span></p>import microApp from @micro-zoe/micro-app

    microApp.start({
    router-mode: native,
    })<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">native-scope模式的功能和用法和native模式<span style="color: black;">同样</span>,<span style="color: black;">独一</span><span style="color: black;">区别</span>点在于native-scope模式下子应用的域名指向<span style="color: black;">自己</span>而非主应用。</p><span style="color: black;">倘若</span>是设置单个子应用,参考如下。
    &lt;micro-app name=xx url=xx router-mode=native-scope&gt;&lt;/micro-app&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>是全局设置,参考如下。</span></p>import microApp from @micro-zoe/micro-app

    microApp.start({
    router-mode: native-scope,
    })<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">pure模式下子应用独立于浏览器进行渲染,即不会修改浏览器<span style="color: black;">位置</span>,<span style="color: black;">亦</span>不会受其影响,其表现和iframe类似。</p><span style="color: black;">倘若</span>是设置单个子应用,参考如下。&lt;micro-app name=xx url=xx router-mode=pure&gt;&lt;/micro-app&gt;
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>是全局设置,参考如下。</span></p>import microApp from @micro-zoe/micro-app

    microApp.start({
    router-mode: pure,
    })<h1 style="color: black; text-align: left; margin-bottom: 10px;">配置项</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">有时候,<span style="color: black;">咱们</span>可能会遇到需要关闭虚拟路由的场景。<span style="color: black;">实质</span>上虚拟路由系统是<span style="color: black;">没法</span>关闭的,<span style="color: black;">这儿</span>的配置只是为了向下兼容旧版本,它的表现和native路由模式一致。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">针对</span>单个子应用<span style="color: black;">来讲</span>,只需要<span style="color: black;">运用</span>下面的代码<span style="color: black;">就可</span>。</span></p>&lt;micro-app name=xx url=xx disable-memory-router&gt;&lt;/micro-app&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而<span style="color: black;">倘若</span>需要全局设置,<span style="color: black;">能够</span>参考下面的代码。</span></p>import microApp from @micro-zoe/micro-app

    // 在start中<span style="color: black;">增多</span>配置
    microApp.start({
    disable-memory-router: true, // 关闭虚拟路由
    })<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">默认<span style="color: black;">状况</span>下,子应用卸载后重新渲染,将和首次加载<span style="color: black;">同样</span>渲染子应用的首页。设置keep-router-state<span style="color: black;">能够</span><span style="color: black;">保存</span>子应用路由状态,在卸载后重新渲染时将恢复卸载前的页面(页面中的状态不<span style="color: black;">保存</span>)。</p><span style="color: black;">倘若</span><span style="color: black;">保存</span>某个子应用的路由状态,参考如下。
    &lt;micro-app name=xx url=xx keep-router-state&gt;&lt;/micro-app&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>需要<span style="color: black;">保存</span>所有子应用的路由状态,参考如下。</span></p>import microApp from @micro-zoe/micro-app

    // 在start中<span style="color: black;">增多</span>配置
    microApp.start({
    keep-router-state: true, // <span style="color: black;">保存</span>路由状态
    })<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">重视</span>:</span></p><span style="color: black;"><span style="color: black;">倘若</span>关闭了虚拟路由系统,keep-router-state<span style="color: black;">亦</span>将失效。</span><span style="color: black;">当设置了default-page时keep-router-state将失效,<span style="color: black;">由于</span>它的优先级<span style="color: black;">少于</span>default-page。</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">3.3 样式隔离</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">MicroApp最初是基于style元素的CSSStyleSheet实现的样式隔离:即将CSS字符串<span style="color: black;">插进</span>style元素生成CSSStyleSheet,遍历<span style="color: black;">每一个</span>CSS规则,添加前缀实现样式隔离。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/983642bcf66446718444d17bc8544e0e~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728795097&amp;x-signature=AQ5HmdJkbjX11pswnHPOjbAwJqE%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这种一种取巧的方式,利用浏览器<span style="color: black;">自己</span>的能力格式化CSS,并<span style="color: black;">这里</span><span style="color: black;">基本</span>上进行修改,省去<span style="color: black;">非常多</span>工作量。但问题<span style="color: black;">亦</span>出<span style="color: black;">此刻</span><span style="color: black;">这儿</span>,<span style="color: black;">区别</span>浏览器<span style="color: black;">针对</span>相同的CSS生成的CSSStyleSheet可能会<span style="color: black;">区别</span>,这就<span style="color: black;">引起</span><span style="color: black;">咱们</span>在处理CSSStyleSheet会遇到不<span style="color: black;">能够</span>预知的问题,<span style="color: black;">引起</span>CSS表现不一致。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">于是MicroApp换了一种方式来实现样式隔离,<span style="color: black;">咱们</span><span style="color: black;">运用</span>正则将CSS字符串切割成最小单元,<span style="color: black;">每一个</span>单元<span style="color: black;">包括</span>一段CSS信息,将所有的信息整理生成CSSTree,遍历CSSTree的<span style="color: black;">每一个</span>规则,添加前缀实现样式隔离。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/5ccaa3dbd8b04bdab31294d9bcbbbf90~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728795097&amp;x-signature=%2FR99%2BmQKQJtJ65tzEGgilKwKmA0%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">3.3.1 样式隔离</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">MicroApp的样式隔离是默认开启的,开启后会以标签<span style="color: black;">做为</span>样式<span style="color: black;">功效</span>域,利用标签的name属性为<span style="color: black;">每一个</span>样式添加前缀,将子应用的样式影响禁锢在当前标签区域。</span></p>.test {
    color: red;
    }

    /* 转换为 */
    micro-app .test {
    color: red;
    }<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">但主应用的样式依然会对子应用产生影响,<span style="color: black;">倘若</span><span style="color: black;">出现</span>样式污染,<span style="color: black;">举荐</span><span style="color: black;">经过</span>约定前缀或CSS Modules方式<span style="color: black;">处理</span>。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">3.3.2 禁用样式隔离</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">禁用样式隔离分四个层次:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>是在所有应用中禁用,<span style="color: black;">能够</span><span style="color: black;">经过</span>start<span style="color: black;">办法</span>进行全局配置,设置后所有应用的样式隔离都会停止。</span></p>import microApp from @micro-zoe/micro-app

    microApp.start({
    disableScopecss: true, // 默认值false
    })<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span><span style="color: black;">期盼</span>在某个应用中不受全局配置<span style="color: black;">掌控</span>,<span style="color: black;">能够</span>设置disableScopecss=false。</span></p>&lt;micro-app name=xx url=xx disableScopecss=false&gt;&lt;/micro-app&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>在某一个应用中禁用,当前应用的所有css都不会进行样式隔离。</span></p>&lt;micro-app name=xx url=xx disableScopecss 或 disable-scopecss&gt;&lt;/micro-app&gt;<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>想要在某一个文件中禁用,<span style="color: black;">能够</span>在你的css文件中<span style="color: black;">运用</span>以下格式的注释来禁用样式隔离。</span></p>/*! scopecss-disable */
    .test1 {
    color: red;
    }
    /*! scopecss-enable */<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">当然,<span style="color: black;">亦</span><span style="color: black;">能够</span>对指定的<span style="color: black;">选取</span>器禁用样式隔离。</span></p>/*! scopecss-disable .test1, .test2 */
    .test1 {
    color: red;
    }
    .test2 {
    color: yellow;
    }
    .test3 {
    color: green;
    }
    /*! scopecss-enable */<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>想在<span style="color: black;">全部</span>文件范围内禁用样式隔离,将 /*! scopecss-disable */ 注释放在文件顶部。</span></p>/*! scopecss-disable */
    ...<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>需要在某一行中禁用,在文件中<span style="color: black;">运用</span>以下格式的注释在某一特定的行上禁用样式隔离,如下所示。</span></p>/*! scopecss-disable-next-line */
    .test1 {
    color: red;
    }

    .test2 {
    /*! scopecss-disable-next-line */
    bac<span style="color: black;">公斤</span>round: url(/test.png);
    }<h1 style="color: black; text-align: left; margin-bottom: 10px;">3.3.3 shadowDOM</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">shadowDOM<span style="color: black;">拥有</span>更好的隔离性,但<span style="color: black;">有些</span>框架(如React)对shadowDOM的兼容性<span style="color: black;">欠好</span>,请<span style="color: black;">小心</span><span style="color: black;">运用</span>。开启shadowDOM后,默认的样式隔离将失效。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">4、</span>Micro-App-DevTools</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Micro-App-DevTools 是基于 MicroApp 推出的一款Chrome浏览器插件,目的是为了在<span style="color: black;">研发</span>和<span style="color: black;">运用</span> MicroApp 过程中提<span style="color: black;">有效</span>率。<span style="color: black;">经过</span>此插件<span style="color: black;">能够</span>有效的<span style="color: black;">处理</span>调试困难、模拟数据通信、查看视窗范围、设置路由、获取环境变量等诉求,<span style="color: black;">从而</span>更好地<span style="color: black;">帮忙</span>用户去<span style="color: black;">认识</span>和<span style="color: black;">运用</span> MicroApp。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/d39264441da8451d844ccccdfb4aee25~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728795097&amp;x-signature=b8WDBs0ha8%2Fe%2BQ5z4sFa1QOL3a4%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/34954eb9cef14d33be63f663f632e787~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728795097&amp;x-signature=cG%2FbiY8gugcG98CO%2BjAljj8MMRs%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Micro-App-DevTools<span style="color: black;">经过</span>模拟子应用<span style="color: black;">研发</span>环境,获取父应用数据,来可视化查看通讯数据,<span style="color: black;">加强</span><span style="color: black;">研发</span>调试效率。<span style="color: black;">针对</span>路由,将会<span style="color: black;">表示</span>所有应用的路由,<span style="color: black;">包括</span>层层嵌套应用以及一个父应用多个子应用的路由,使<span style="color: black;">区别</span>团队应用<span style="color: black;">亦</span>能快速定位自己问题,方便协作。还<span style="color: black;">供给</span>了全局变量和高亮视窗功能,实现快速定位范围,<span style="color: black;">加强</span>排查效率的功能,并集结了图标、右键、<span style="color: black;">掌控</span>台的快捷进入方式,<span style="color: black;">运用</span>户快速上手,零成本<span style="color: black;">运用</span>。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-twdt4qpehh/287eea101997464a99ba596949d10b1f~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728795097&amp;x-signature=Ec6NZrHNPnsS1xkSOa2ZZGYculY%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">参考链接:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MicroApp Github<span style="color: black;">位置</span>:</p>https://github.com/micro-zoe/micro-app

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Micro-App-DevTools Github<span style="color: black;">位置</span>:</p>https://github.com/micro-zoe/micro-app-chrome-plugin

    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MicroApp官网<span style="color: black;">位置</span>:</p>https://micro-zoe.github.io/micro-app





qzmjef 发表于 2024-10-16 14:43:42

交流如星光璀璨,点亮思想夜空。
页: [1]
查看完整版本: 京东微前端框架MicroApp简介