0

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?

1

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I would like to show a bunch of externally loaded components on the same page.
0

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 .

2 Comments

Using require will include the component in build time
Indeed, however it will give you ability to config components to use e.g. in JSON config file etc.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.