【问题标题】:Nested routing in Angular 2/4Angular 2/4 中的嵌套路由
【发布时间】:2017-04-13 10:52:13
【问题描述】:

我正在开发一个我打算具有以下结构的应用程序:

-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)    
--…

它是应用程序的起始框架,将有 3 个级别 - 主、应用程序和子应用程序。每个应用程序和子应用程序都将独立开发。将来可能会有更多延迟加载的关卡。 我希望在每个级别上独立维护路由,这意味着 MAIN 将处理 NCF 和 App2 和 App3 的路由(主要用于延迟加载); NCF 将处理 ACNP、SubApp2 和 SubApp3 的路由(同样主要是延迟加载); ACNP 将处理其组件的路由。 这是它现在的样子:

主路由.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>

主导航

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

它工作正常,在 MAIN-nav 我有延迟加载 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(现在我只想加载一个默认组件)::

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

这是我的问题开始的地方:当我点击加载 ACNP 时,它被加载,但它呈现在第一个路由器出口下方(在 MAIN 级别),但我希望它呈现在第二个路由器出口下方,在 nfc.html 中定义.

我尝试使用命名路由器插座来做到这一点:

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'
  }
]

但是当我点击加载 ACNP 时出现错误:

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

如何使组件在最近的路由器插座下方呈现?

【问题讨论】:

    标签: angular angular2-routing


    【解决方案1】:

    您需要更新您的ncfRoutes.ts,以便acnp 是不在同一级别的子路由,

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

    看到这个Plunker,它有一个解决三级路由类似问题的方法。

    希望这会有所帮助!

    【讨论】:

      【解决方案2】:

      Angular 8 中,您可以创建单独的 RoutingModules,其中的组件具有自己的模块。

      在 app-routing.module.ts 文件中使用 loadChildren 加载子组件的模块

      const routes: Routes = [
          { path: 'dashboard', component: DashboardComponent },
          { path: 'profile', component: ProfileComponent },
          { path: 'attendance', component: AttendanceComponent },
          { path: 'leaves', loadChildren: () => import(`./leaves/leaves.module`).then(m => m.LeavesModule) },
          { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
          { path: '**', component: Page404Component },
      ];
      

      查看完整教程here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-09
        • 1970-01-01
        • 2018-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-14
        相关资源
        最近更新 更多