7

I have been lazy loading modules in routes e.g.

export const HomeRoute: Route = {
  path: '',
  component: HomeComponent,
  canActivate: [AuthGuard],
  children: [
    {path: 'dashboard', loadChildren: 'app/+dashboard/db.module#DashboardModule'}
  ]
};

I would like to put my "pages" into NPM modules. What is the route to the node_module that I should use in the loadChildren attribute? I am using angular-cli 1.0.0-beta.16

I have tried

{path: 'lazy', loadChildren: '../node_modules/hello-world/components#HelloWorld' }

also

{path: 'lazy', loadChildren: 'hello-world/components#HelloWorld' }

The exported class is: -

import {Component} from '@angular/core';

@Component({
    selector: 'hello-world',
    styles: [`
       h1 {
            color: blue;
        }
    `],
    template: `<div>
                  <h1 (click)="onClick()">{{message}}</h1>
               </div>`
})
export class HelloWorld {

    message = "Click Me ...";

    onClick() {
        this.message = "Hello World!";
        console.log(this.message);

    }
}

Is there anything else I should try?

2 Answers 2

2

This currently isn't possible - see a response from the AngularJS CLI team here: -

https://github.com/angular/angular-cli/issues/2601

"This is a very relevant question. I don't think we support it in the CLI atm." (Currently version beta 17)

Datumgeek has implemented lazy loading from modules in a different way (outside of CLI) here: - https://github.com/datumgeek/a2dyn/blob/master/README.md#development-server

I will update the answer if it becomes possible in the Angular CLI in the future

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

2 Comments

Do you know if it is possible to do this without CLI? I.e. Basing on angular webpack starter from angularclass? I tried to link to my modules via '../node_modules/path_to_my_module' and the only thing which is not working is AOT compilation.
A workaround can be found here (one of comment at about the end of the issue): github.com/angular/angular-cli/issues/6373, but this one is not lazy load, neither AOT compilation support.
0

check that HelloWorld is an exported class and not a 'default export'. otherwise it won't work.

2 Comments

Apologies for the delay, I was off on holiday. Yep, the HelloWorld class is just exported, I've edited the question to show the class in full
It might be an issue with angular-cli not exposing webpack configuration as it's mentioned here that he had to use webpack directly rather than CLI github.com/datumgeek/a2dyn/blob/master/…

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.