简体   繁体   中英

Mixing components and modules in angular routing

I am working on a legacy angular app, and slowly have upgraded it to ng v9. Currently, i have such a routing config that loads only components but would like to transition to modules starting now.

This is how my current routing file looks like

const routes = [
  {path: 'home', component: HomeComponent, canActivate:[routeGuard]},
  {path: '', redirectTo: 'home', pathMatch: 'full'},
  {path: 'settings', loadChildren: () => import('path_to_import').then(mod => mod.SettingsModule)}
]

and from the component where am trying to navigate its simply like so -

<span routerLink="settings"></span>

Wierdly, when i click on settings on the page, it is redirecting me to '/settings/home' which is not the intended behavior.

But when i change the module to be loaded as component like this -

const routes = [
  {path: 'home', component: HomeComponent, canActivate:[routeGuard]},
  {path: '', redirectTo: 'home', pathMatch: 'full'},
  {path: 'settings', component: SettingsComponent}
]

it works perfect. so am not sure why loading module is making the difference here.

Any help appreciated!

You need to have the routing for your settings module also. Which would decide the further routing of the project.

and instead of loading modules like you mentioned, try to add the below lines to load the module settings .

const routes = [
{path: 'home', component: HomeComponent, canActivate:[routeGuard]},
{path: '', redirectTo: 'home', pathMatch: 'full'},
{path: 'settings', loadChildren: './settings/settings.module#SettingsModule'}
]

I don't know the path of your settings module. You please correct as per your path. and also define the routing file for the settings module also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM