ErRam
ErRam

Reputation: 19

Lazy loading components in angular2

I am having three components that load simultaneously when angular app load. Is there any way that we can load component only when specific route navigate to.

Upvotes: 1

Views: 3482

Answers (1)

David M.
David M.

Reputation: 2640

This is what the Angular 2 Router is all about. I strongly suggest you read the documentation on Router thoroughly.

https://angular.io/docs/ts/latest/guide/router.html

The steps you need to do are roughly the following:

  1. Create a main component (ex: my-app) for your app with a <router-outlet> placeholder within its template.
  2. Create your routes
  3. Register those routes in your main application module
  4. Add a reference to your main component (<my-app></my-app>) in your index.html file
  5. Open up one of the URLs you registered as a route and the component you associated with that route will get created and inserted in place of your <router-outlet> element.

Upvotes: 1

Related Questions