bjorkblom
bjorkblom

Reputation: 1959

Load external Angular 2 component/module in runtime

Anyone know if it's possible to load an external js file containing an Angular 2/4 module/component?

If so, how do you do, and how is that component/module compiled to that external js?

Upvotes: 0

Views: 824

Answers (2)

proti
proti

Reputation: 213

It is possible. Look here: https://blog.thecodecampus.de/angular-2-dynamically-render-components/ Instead of entering entryComponents array upfront I would try to do some magic with require('path_to_component').default .

Upvotes: 0

Aniruddha Das
Aniruddha Das

Reputation: 21698

LoadChildren in routing is one of the best way used to load module and components in to angular project. If you are using cli, webpack will build your modules seperatly and they will be loaded dynamically at run time using router.

const routes: Routes = [
  { path: '', loadChildren: './some-folder/private.module#PrivateModule'},
  { path: 'login', component: LoginComponent }
];

Here for the blank route the private module, private component will be loaded dynamically.

Upvotes: 1

Related Questions