在Angular中,通过使用命名的路由出口,可以让一个路由在多个地方同时显示。但是,在给命名的路由出口添加路径时需要注意,在路径前面加上斜杠"/"。
以下是示例代码:
// app-routing.module.ts const routes: Routes = [ { path: '', component: HomeComponent }, { path: 'login', component: LoginComponent }, { path: 'dashboard', component: DashboardComponent, children: [ { path: 'overview', component: OverviewComponent }, { path: 'reports', component: ReportsComponent, outlet: 'header' } ] } ];
在上面的代码中,我们创建了两个路由出口。其中一个是未命名的默认路由出口,另一个是命名为“header”的路由出口。注意到在“ReportsComponent”的路由配置中,我们使用了“outlet”属性来指定该路由应该显示在哪个命名出口中,并且我们还添加了斜杠“/”来修正路由路径。