angular - 阻止用户导航到功能模块内的父路由
问题描述
我有一个带有 2 个功能模块的 Angular 10 应用程序。一个用于登陆页面,可以通过 de ' '
route 访问,延迟加载功能模块LandingPageModule
。第二个是仪表板,可以通过'/dashboard'
路由到达,延迟加载功能模块DashboardModule
。
在里面DashboardModule
,我需要一个侧边栏在用户的整个导航过程中保持可见。我已经使用子路由来实现这一点,因此可以在父组件内添加侧边栏,并使用 处理子路由<router-outlet>
,允许用户在子路由中导航。
中有 3 条子路线DashboardRoutingModule
:
'/dashboard/summary'
加载SummaryComponent
'/dashboard/bookings'
加载BookingListComponent
'/dashboard/clients'
加载ClientListComponent
我制作了路线工作流程的这个模式来完成我的解释。
在这里您可以找到实现此目的的代码
应用路由模块
const routes: Routes = [
{ path: '', loadChildren: () => import('./landing-page/landing-page.module').then(m => m.LandingPageModule) },
{ path: 'dashboard', loadChildren: () => import('./dashboard/dashboard.module').then(m => m.DashboardModule) },
{ path: '**', redirectTo: '' }
];
app.component.html
<router-outlet></router-outlet>
登陆页面路由模块
const routes: Routes = [{ path: '', component: LandingPageComponent }];
仪表板路由模块
const routes: Routes = [
{
path: '', component: DashboardComponent,
children:
[
{ path: 'summary', component: SummaryComponent },
{ path: 'bookings', component: BookingListComponent },
{ path: 'clients', component: ClientListComponent },
]
},
{ path: '**', redirectTo: '', pathMatch: 'full' }
];
仪表板.component.html
<div id="wrapper">
<app-sidebar></app-sidebar>
<div id="content">
<router-outlet></router-outlet>
</div>
</div>
我的问题是
当用户导航到 时/dashboard
,他会看到侧边栏和旁边的空白页面,因为 . 下方没有添加任何组件<router-outlet>
。
我的问题是
我应该怎么做才能防止用户手动导航到/dashboard
?
如果我使用redirectTo
,DashboardComponent
则未加载并且子路由根本不起作用。
另一个好的解决方案是删除/dashboard/summary
子路由。当用户导航到/dashboard
时,它将加载SummaryComponent
路由器的插座并保持侧边栏可见。但我找不到任何方法让它像那样工作。
解决方案
我只需要添加另一个处理' '
.
仪表板路由模块
const routes: Routes = [
{
path: '', component: DashboardComponent,
children:
[
{ path: 'summary', component: SummaryComponent },
{ path: 'bookings', component: BookingListComponent },
{ path: 'clients', component: ClientListComponent },
{ path: '', pathMatch: 'full', redirectTo: 'summary'} //New child route handling ''
]
},
{ path: '**', redirectTo: '', pathMatch: 'full' }
];
推荐阅读
- gradle - 无法编译使用 gradle-swagger-generator-plugin 生成的代码
- python - 如何跟踪视频中检测到的特定人员?
- linux - 在交叉编译中链接二级依赖项
- python - 在 Pyspark 中获取 pandas Dataframe.to_numpy() 的另一种方法
- node.js - 如果 API 输出中存在特殊字符,则使用 axios 调用 API 失败
- laravel - Laravel DotEnv 无法从 AWS Beanstalk 的控制台读取环境
- html - 使用 ngStyle 为粘性列设置 Angular 材质数据表标题左侧 CSS 属性?
- matlab - 具有复数显示格式的符号函数
- php - WPBakery 单篇博客文章模板
- .htaccess - htaccess url从请求查询重写到文件夹,并更改php名称