Search code examples
angulardynamicangular-bootstrap

Can i import some custom module after a angular 2 app bootstraped?


I am developing a business web app using angular2 based on web api. Now i got a problem. I want to bootstrap my angular2 app using AuthModule(this is my custom module). This module have functionalty to perform login and signup operation. I don't want to bootstrap my full angular2 app when a user not logged in. I want to import all modules and components After a user successfully logged in. Just like that..

In main.ts

import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);

after login success,

// i need to import my modules after login
//platform.importModule([MyCustomModule]);.....

I don't know this is possible or not. I don't see any API documentation in angular.io site. Please give me any idea if this is possible.

Thanks.


Solution

  • why you are using platform.importModule for loading your customModule. you can use lazy loading of angular 2 for this.

    here are some useful links:

    https://angular-2-training-book.rangle.io/handout/modules/lazy-loading-module.html

    https://angular.io/docs/ts/latest/guide/router.html in this see how they have used lazy loading.

    study from these links how to use lazy loading.