1

So I've got a pretty simple routing situation setup, in which I'm lazy loading modules:

const routes: Routes = [
  {
    path: '',
    loadChildren: './components/top-accounts/top-accounts.module#TopAccountsModule',
  },
  {
    path: 'tags',
    loadChildren: './components/tags/tags.module#TagsModule',
  },
  {
    path: ':accountId/details',
    loadChildren: './components/account-details/account-details.module#AccountDetailsModule',
  },
  { path: '**', redirectTo: '/' }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})

However, when I refresh the page, my routing gets messed up and it tries to default to the / route, even though the URL says something like /tags or /accountId1/details and then it will also give me an error about loading chunks or the page is straight up blank with no error

If I'm trying to get the routes to redirect correct on page load AND show the correct urls AND not give me errors about failing to load lazy loaded module chunks, is there something simple I need to do?

I'm currently on Angular 7.2.0

Stevie Star
  • 2,331
  • 2
  • 28
  • 54
  • You should specify a default route { path: "", redirectTo: "account", pathMatch: "full" }, could you provide your routing inside tagsmodule for example ? – Caro Feb 27 '20 at 14:36

1 Answers1

2

Please try that:

RouterModule.forRoot(routes, { useHash: true })

instead of this:

imports: [RouterModule.forRoot(routes)]

that:

{ path: '**', redirectTo: '/', pathMatch: 'full' }

instead of this:

{ path: '**', redirectTo: '/' }

and take a look for more info here:

Angular2 without hash in the url and Need clarification RouterModule.forRoot([ ABOUT_ROUTE ], { useHash: true })

Giannis
  • 1,790
  • 1
  • 11
  • 29
  • Hey thanks for this answer, I was able to get it working using `{ path: '**', redirectTo: '/', pathMatch: 'full' }` and I didn't have to change `imports: [RouterModule.forRoot(routes)]` :) For future reference, is there a way to do this without using the `useHash` that you know of? – Stevie Star Feb 27 '20 at 15:53