簡體   English   中英

如何動態加載Angular2根組件

[英]How to load Angular2 root component Dynamically

我有一個要求,我必須動態地/使用setTimeout()將根組件加載到angular2中。 是可能的,以及如何。 例如

<body>
 <hello-world>Loading...</hello-world>
</body>

我希望在頁面加載幾秒鍾后追加“ hello-world ”。 由於我無法在頁面加載中包含自定義組件。

plnkr會有所幫助。

您可以在索引頁面上添加事件,並在main.ts角度文件中偵聽此事件。

index.html:

...  
<body>
    <my-app>Loading...</my-app>

    <script>
      setTimeout(function() {
          document.dispatchEvent(new Event('startNgLoad'))
      }, 5000)
    </script>
  </body>
...

main.ts:

document.addEventListener('startNgLoad', () => {
  platform.bootstrapModule(AppModule);
})

您可以在main.ts中的setTimeout中加載根模塊

setTimeout(function() {
  platformBrowserDynamic().bootstrapModule(AppModule);
},1000)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM