Nested routing in Angular 2/4

2019-05-12 08:59发布

I work on an app which I intend to have following structure:

-MAIN -  main “container” (main routes)    
--NCF (lazy loaded, routes for it’s subapps)    
---ACNP (lazy loaded)    
----Component1    
----Component2    
---SubApp2 (lazy loaded)    
---SubApp3 (lazy loaded)    
--App2 (lazy loaded)    
--App3 (lazy loaded)    
--…

It’s starting framework for app which will have 3 levels – MAIN, apps and subapps. Every app and subapp is to be developed independently. In future there may be more lazy-loaded levels. I want routing to be maintained independently on each level, which means MAIN will handle routes (for lazy loading mainly) for NCF and App2 and App3; NCF will handle routes (again mostly lazy loaded) for ACNP, SubApp2 and SubApp3; ACNP will handle routes for its components. Here’s how it looks right now:

MAIN-routes.ts:

import {Routes} from "@angular/router"

export const appRoutes: Routes = [
  {
    path: 'ncf',
    loadChildren: './ncf/ncf.module#NewCustomerFolderModule
  }
]

MAIN.html

<h1>FE2</h1>
<MAIN-nav></MAIN-nav>
<router-outlet></router-outlet>

MAIN-nav

<a [routerLink]="['/ncf']">New Customer Folder</a>

And it works fine, inside MAIN-nav I have links which lazy load NCFModule.

NCFModule.ts

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(ncfRoutes)
  ],
  declarations: [NewCustomerFolderComponent]
})

ncfRoutes.ts

import {Routes} from "@angular/router"
import { NCFComponent } from "./ncf.component"

export const ncfRoutes: Routes = [
  {
    path: '',
    loadChildren: './acnp/acnp.module#AddCustomerNaturalPersonModule
  },
  {
    path: '',
    component: NCFComponent
  }
]

ncf.html

<hr>
<a [routerLink]="['acnp']">Load ACNP</a>
<p>test</p>
<router-outlet></router-outlet>

acnp.routes (right now I just want to load one component on default)::

export const acnpRoutes: Routes = [
  {
    path: '',
    component: ACNPStepOneComponent
  }
]

Here’s where my problem begin: When I click on Load ACNP it is loaded, but it renders below first router-outlet (on MAIN level), but I want it to render below second router-outlet, defined in nfc.html.

I tried to do it using named router outlets:

ncf.html

<hr>
<a [routerLink]="['acnp', {outlets: {NCFRouterOutlet: ['acnp']}}]">Load ACNP</a>
<p>test</p>
<router-outlet name="NCFRouterOutlet"></router-outlet>

acnp.routes 
export const acnpRoutes: Routes = [
  {
    path: '',
    component: ACNPStepOneComponent,
    outlet: 'NCFRouterOutlet'
  }
]

But hen when I click on Load ACNP I get error:

core.es5.js:1084 ERROR Error: Uncaught (in promise): Error: Cannot find the outlet NCFRouterOutlet to load 'ACNPStepOneComponent' 

How can I make components render below closest router-outlet?

1条回答
Bombasti
2楼-- · 2019-05-12 09:43

you need to update your ncfRoutes.ts so that acnp is a child route not on the same level,

export const ncfRoutes: Routes = [      
  {
    path: '',
    component: NCFComponent,
    children: [
           {
             path: 'acnp',
             loadChildren: './acnp/acnp.module#AddCustomerNaturalPersonModule
            }
     ]
  }
]

See this Plunker, it has a solution for similar problem with three level of routes.

Hope this helps!!

查看更多
登录 后发表回答