繁体   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