在Angular中,可以使用children
属性来定义子路由。以下是如何在延迟加载的子路由中定义父路由的代码示例:
在父路由模块中,定义父路由和子路由:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{ path: 'parent', loadChildren: () => import('./parent/parent.module').then(m => m.ParentModule) },
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
在子路由模块中,定义子路由和父路由:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ParentComponent } from './parent.component';
import { ChildComponent } from './child.component';
const routes: Routes = [
{
path: '',
component: ParentComponent,
children: [
{ path: 'child', component: ChildComponent },
]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class ParentRoutingModule { }
注意,在父路由模块中使用loadChildren
来延迟加载子路由模块。在子路由模块中使用forChild
方法来定义子路由。
希望以上解决方法能帮到你!