0

I have a lazy loaded module AccountModule which gets loaded after accessing https://localhost:4201/account.

app.routing.ts

const routes: Routes = [

  {
    path: '',
    loadChildren: () => import('./ui/pages/start/start.module')
      .then(m => m.StartModule)
  },


  {
    path: 'account',
    loadChildren: () => import('./ui/pages/account/account.module')
      .then(m => m.AccountModule)
  },

];

export const AppRouting = RouterModule.forRoot(routes, {
  useHash: false,
  enableTracing: false
});

AccountModule has two child routes. One of which gets loaded eagerly (/overview) whereas the other gets loaded lazily (/albums).

Hence, acount.routing.ts looks like this:

const routes: Routes = [

  {path: '', redirectTo: 'overview', pathMatch: 'full'},

  {
    path: 'overview',
    component: AccountComponent,
  },

  {
    path: 'albums',
    loadChildren: () => import('./albums/albums.module').then(m => m.AlbumsModule),
    // component: AlbumsComponent  // This works!
  },

];

export const AccountRouting = RouterModule.forChild(routes);

Now, if I use "eager" loading (component:AlbumsComponent) everything is working. But, if I switch to loadChildren: () => ... routing does not work anymore. This routerLink

<app-raised-button [routerLink]="['/account/albums']">
  My Albums
</app-raised-button>

Results in https://localhost:4201/account/albums (working) or https://localhost:4201/account/albums/overview (not working). But why? And how can I fix this issue?

Why does it behave differently just because of the way the module/component gets loaded?


If I am setting the path to ./albums:

{
  path: './albums',
  loadChildren: () => import('./albums/albums.module').then(m => m.AlbumsModule),
},

I am just getting

Error: Cannot match any routes. URL Segment: 'account/albums'

1
  • What happens if you try: loadChildren: './albums/albums.module#AlbumsModule' ? Commented Jan 19, 2020 at 11:59

2 Answers 2

2

I guess you have not provided a Routes for albums.routing.module ?

Since you have directed the albums.module, but you might have not provided from there what component you have been show.

If you have not created to albums.routing.module, create one and provide a default route in the same module.

const routes: Routes = [

  {path: '', component:AlbumsComponent } ];
Sign up to request clarification or add additional context in comments.

2 Comments

+facepalm+ .. yes, this was indeed the problem. I forgot to provide routes for the module.. Thanks for helping!
glad to help you.. :)
0

Make sure in your albums module , you have your have imported your routes as shown : RouterModule.forChild(albumsRoutes)

2 Comments

Nope, still getting account/albums/overview - no idea why this happens ..
Make sure in your albums module , you have your have imported your route as RouterModule.forChild(albumsRoutes)

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.