1. 程式人生 > 其它 >web元件化 - 微前端,動態指定web component標籤

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>