angular - 如何在角度路由模块中按条件重定向通配符路由
问题描述
当用户使用通配符键入错误的 URL 但通过条件控制时,是否有办法处理重定向路由?
我的项目分别有3个主要模块,每个模块都有一些只能由用户角色访问的条件。
问题是我不知道如何按条件处理它,我试过 canActivate 但它不起作用
main.ts:14 错误:路由“**”的配置无效。必须提供以下之一:component、redirectTo、children 或 loadChildren
这是我的一些代码。
const routes: Routes = [
{
path: '',
pathMatch: 'full',
redirectTo: 'customer1'
},
{
path: 'customer1',
component: RootOneComponent,
children: [
{
path: '',
pathMatch: 'full',
redirectTo: 'home'
},
{
path: 'home',
component: HomeOneComponent
},
{
path: '**',
component: PageNotFoundComponent
}
],
},
{
path: 'customer2',
component: RootTwoComponent,
children: [
{
path: '',
pathMatch: 'full',
redirectTo: 'home'
},
{
path: 'home',
component: HomeTwoComponent
},
{
path: 'BlogPicture',
loadChildren: () => import('./blog/blog.module').then(m => m.BlogModule)
},
{
path: '**',
component: PageNotFoundComponent
}
],
},
{
path: 'customer3',
component: RootThreeComponent,
children: [
{
path: '',
pathMatch: 'full',
redirectTo: 'home'
},
{
path: 'home',
component: HomeThreeComponent
},
{
path: 'blog-price',
loadChildren: () => import('./blog/blog.module').then(m => m.BlogModule)
},
{
path: '**',
component: PageNotFoundComponent
}
],
},
// wild card condition is there the way to manage this?
{
path: '**',
redirectTo: 'classic'
}
];
解决方案
在你的 app.routing.module.ts
const routes: Routes = [{
path: "customer1",
component: FirstCustomerModule,
pathMatch: "full"
},
{
path: "customer2",
data: { preload: true },
loadChildren: () => SecondCustomerModule
},
{
path: "customer3",
data: { preload: true },
loadChildren: () => ThirdCustomerModule
}
}
path 定义了您提到的路径,并且在模块中您必须提到提到的模块将被命中。那么在你的 Modules 中你必须相对提及路径,我刚才提到了应用级路由,那么你可以进一步实现模块级路由,Load Children 告诉我们的应用它是一个模块并且它有更多的组件
推荐阅读
- javascript - 为什么 console.time 显示的时间比实际时间短得多?
- ubuntu - Artifactory 启动失败
- api - Mulesoft 与 api 和数据库的集成
- laravel - 两个 select2_ajax 过滤器之间的关系
- php - foreach 循环内的条件检查
- apache-kafka - 您是否需要多个 zookeeper 实例来运行多代理 kafka?
- eclipse - 如何增加 Websphere 8.5.5 的启动超时在 Intellij 中,我知道在 Eclipse 中我们可以双击服务器?
- swagger - fastapi[openapi.json]- 招摇分页问题
- c# - 如何使用 MassTransit 从 RabbitMQ 代理接收集成事件?
- node.js - 刷新页面后本地存储数据重置