在Angular中,可以通过使用{ path: '', pathMatch: 'full', redirectTo: 'child-route' }
来忽略父路径。这样,当导航到父路径时,它会自动重定向到子路径。
下面是一个示例代码:
在父组件的路由模块中(如app-routing.module.ts
):
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ParentComponent } from './parent.component';
const routes: Routes = [
{
path: '',
pathMatch: 'full',
redirectTo: 'child-route'
},
{
path: 'child-route',
component: ParentComponent,
loadChildren: () => import('./child/child.module').then(m => m.ChildModule)
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class ParentRoutingModule { }
在子组件的路由模块中(如child-routing.module.ts
):
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ChildComponent } from './child.component';
const routes: Routes = [
{
path: '',
component: ChildComponent
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class ChildRoutingModule { }
请注意,上述示例假设有一个名为ChildComponent
的子组件,其对应的模块为ChildModule
。你需要相应地调整和配置你自己的父组件和子组件。
下一篇:Angular类绑定