web组件化 - 微前端,动态指定web component标签

第一篇介绍了如何将React组件转换为Web Component
第二篇介绍了子应用(Web Component)中的路由可以正常作用与Shell App
第三篇介绍了Sub App与Shell App通过属性或自定义事件交互
第四篇介绍Web Component + React实现微前端的POC
第五篇子应用Webpack排除React依赖包
第六篇子应用的样式隔离

在之前的基础上再对shell app做些优化,动态指定web component标签tag
删除原有的SubApp01, SubApp02, 替换为:

const config = {
  app01: {tag: 'hello-component', url: 'http://localhost:5001/bundle' },
  app02: {tag: 'sub-app-02', url: 'http://localhost:5002/bundle' }
}

function SubApp(props) {
  const externalScript = config[props.appId].url;
  const Tag = config[props.appId].tag; //此处必须大写
  const loadingState = useScript(externalScript);
    return(
    <div>
      {loadingState === "loading" && <p>Loading...</p>}
      {loadingState === "ready" && <Tag />}
    </div>
  );
}

调用处替换为:

<SubApp appId={app}></SubApp>
posted @ 2021-10-29 18:03  老胡Andy  阅读(260)  评论(0编辑  收藏  举报