1

I have a pretty simple Angular2 RC5 test project which attempts to use routing to lazy load modules. I can't see what's wrong with the project.

Folders

app
  |-about (contains about.module.ts and about.component.ts)
  |-home (contains home.module.ts, home.component.ts)

app.routing.ts

export const routes: Routes = [
    { path: '', redirectTo: 'home', pathMatch: 'full' },
    { path: 'home', loadChildren: './app/home/home.module' },
    { path: 'about', loadChildren: './app/about/about.module' }
];

export const routing = RouterModule.forRoot(routes);

home.module.ts

import { NgModule }         from '@angular/core';
import { HomeComponent }    from './home.component';

@NgModule({
    declarations: [HomeComponent],
    exports: [HomeComponent]
})

export default class HomeModule { }

home.component.ts

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

@Component({
    template: `<h2>Home...</h2>`
})

export class HomeComponent { }

I know the redirect to "home" is working, at startup I can see that home.module.js and home.component.js files are downloaded, but then it throws a console error message Cannot find 'HomeModule' in './app/home/home.module' (and in the network traffic tab I can confirm that both of the js files are correct, spelling is correct, etc.)

1
  • I suppose I should also mention I'm using router 3.0.0-rc.1, which is the latest as of Angular2 RC5, as far as I know. Commented Aug 16, 2016 at 22:41

1 Answer 1

2

Just replace:

{ path: 'home', loadChildren: './app/home/home.module#HomeModule' },

Into this line:

{ path: 'home', loadChildren: './app/home/home.module' },

If you already add export default in your module file, you do not have to specify name in path.

Of course, if you forgot about "default", the only way to select proper module is to add #name attribute - this is the second way. You could use one of them, not both.

And also in your HomeModule, add:

imports: [ 
    RouterModule.forChil‌​d([ 
       { path: '', component: HomeComponent } 
    ]) 
], 

Because all modules could have few components (not only one) so it is necessary to show angular a path to your main component.

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

7 Comments

Thanks, but if I remove either default (and only "default", keeping export class HomeModule) or if I remove the #name tag, it hangs for awhile then blows the stack. If I remove export default (leaving only class HomeModule and retain the #name tag, I get the same Cannot find module error.
No no, you can't remove export word. Try to remove #HomeModule and keep stay "default". So - { path: 'home', loadChildren: './app/home/home.module' } and export default class HomeModule. Do you have any error message now?
Sure, editing now. Stack error: imgur.com/q2BM7r9 (Sorry, previous reply was wrong -- if I only remove default or if I only remove the #name tag in the route, it blows the stack.)
In your HomeModule, add: imports: [ RouterModule.forChild([ { path: '', component: HomeComponent } ]) ],
Because you only tell angular which module should be loaded. All modules could have few components (not only one) so it is necessary to show angular a path to your main component :)
|

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.