Angular 中的嵌套路由 2/4
Nested routing in Angular 2/4
我正在开发一个我打算具有以下结构的应用程序:
-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 将处理其组件的路由。
这是它现在的样子:
主要-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>
主导航
<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'
}
]
但是当我点击 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 8 中,您可以创建单独的 RoutingModule,其中的组件具有自己的模块。
在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
我正在开发一个我打算具有以下结构的应用程序:
-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 将处理其组件的路由。 这是它现在的样子:
主要-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>
主导航
<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'
}
]
但是当我点击 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 8 中,您可以创建单独的 RoutingModule,其中的组件具有自己的模块。
在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