gpt4 book ai didi

angular - Angular 2/4 中的嵌套路由

转载 作者:太空狗 更新时间:2023-10-29 18:13:34 24 4
gpt4 key购买 nike

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

-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 中定义.

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

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

但是当我点击 Load ACNP 时,我得到了错误:

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

如何让组件呈现在最近的路由器导出下方?

最佳答案

你需要更新你的ncfRoutes.ts,使acnp成为不在同一层级的子路由,

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

查看此 Plunker , 它有解决类似问题的三级路由。

希望这对您有所帮助!

关于angular - Angular 2/4 中的嵌套路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43390302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com