16

I've splitted my app into two modules: one with main basic functionality and other with less-used features like account settings, faq pages and more.

What I'm trying to accomplish is to lazy load the second module for some root route paths, like /account or /settings without having to create many different modules. As far as I know Angular lazy load only works with one root route, and the routes configured in the lazy loaded module are set as children of that route.

 {
        path: 'account',
        loadChildren: './modules/settings/settings.module#SettingsModule',
 },
 {
        path: 'settings',
        loadChildren: './modules/settings/settings.module#SettingsModule',
 },
9
  • you need to make sure the route of your lazy loaded module contain the two paths your are calling Commented Mar 22, 2018 at 18:21
  • How? Can you post an example? Because with this configuration, the lazy loaded module only "see" an empty string, as /account or /settings are consumed by the root router Commented Mar 22, 2018 at 18:39
  • Hello, I have the same issue, did you resolve it ? Commented Feb 28, 2019 at 16:26
  • You cannot do it in the current Angular version without manually loading the lazy modules and render the apropiate components. Hopefully this will be solved with Angular Ivy Commented Feb 28, 2019 at 16:28
  • @JavierMarín Did you see anywhere that they say this will be solved with Ivy? Commented Apr 12, 2019 at 11:32

3 Answers 3

1

The Angular team is working on standalone components, that could potentially make loading some rarely used parts less painful:

https://blog.angular.io/an-update-on-standalone-components-ea53b4d55214

One easy (although not 100% optimal) way that is good enough for a lot of requirements probably is using a single shared module - that way your extra overhead is "just" to create another feature module

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

Comments

0

To create an instance of a component in a lazy loaded module without the router, this snippet could help:

class LazyLoader {
    constructor(private _injector: Injector,
                private _moduleLoader: NgModuleFactoryLoader) {
    }

    public loadLazyModule() {
        this._moduleLoader.load('./modules/settings/settings.module#SettingsModule')
            .then((moduleFactory: NgModuleFactory<any>) => {
                const moduleRef = moduleFactory.create(this._injector);

                // Here you need a way to reference the class of the component you want to lazy load
                const componentType = (moduleFactory.moduleType as any).COMPONENT_CLASS;
                const compFactory = moduleRef.componentFactoryResolver.resolveComponentFactory(componentType);
                const componentRef = container.createComponent(compFactory);

                // Instance of the lazy loaded component
                componentRef.instance 
            })
    }
}

Comments

0

The truth is that, unless you do something dynamically loaded, like in the answer above, you cannot do this. Angular is not intended to work, out of the box, with this type of structure. This is a common problem and is already being addressed for future releases, with the use of standalone components (see https://netbasal.com/aim-to-future-proof-your-standalone-angular-components-accb574d273f)

So, for your case, I think the best approach would be break your main module into core, and shared modules, and create a separate module for each feature module (account settings, faq pages, ...).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.