1

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.

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

1 Comment

What if we don't want to use router to do the component-lazy-loading? Let say the module and one of the 3 components are eagerly loaded, how do we lazily load the other 2 components on-demand? I heard Angular 4 can handle this more straightforward, but I haven't really dig into this and it's still experimental.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.