路由参数异常(特殊排除)
Exception (special exclusion) in route parameters
我的 app-routing.module.ts
中有以下路由定义:
{ path: 'messages/:id', loadChildren: './pages/message/message.module#MessagePageModule' },
但是,我有一个加载存档邮件的页面。自然地,此页面的路径类似于 messages/archived
。但是,这与已经使用消息页面 ID 定义的路由冲突,即 messages/:id
.
我如何定义一个异常,如果 id == 'archived'
,它应该加载包含存档消息的页面?
更多信息:消息中的 ID 始终是数字,即在实时中它类似于 messages/123
。
建议:您应该对存档消息使用查询参数,因为这是消息类型而不是不同的资源,而 :id 是单一资源。
解决方法:更改存档路由的顺序如下:
在应用程序中-routing.module.ts
{ path: 'messages', loadChildren: './pages/message/message.module#MessagePageModule' }
留言中-routing.module.ts
const messageRoutes: Routes = [
{
path: '',
children: [
{
path: 'archived',
component: ArchivedComponent
},
{
path: ':id',
component: MessageComponent
},
{
path: '',
component: MessageComponent,
pathMatch: 'full'
}
]
}
正如路线顺序所解释的那样here。这应该可以解决您的问题。
我的 app-routing.module.ts
中有以下路由定义:
{ path: 'messages/:id', loadChildren: './pages/message/message.module#MessagePageModule' },
但是,我有一个加载存档邮件的页面。自然地,此页面的路径类似于 messages/archived
。但是,这与已经使用消息页面 ID 定义的路由冲突,即 messages/:id
.
我如何定义一个异常,如果 id == 'archived'
,它应该加载包含存档消息的页面?
更多信息:消息中的 ID 始终是数字,即在实时中它类似于 messages/123
。
建议:您应该对存档消息使用查询参数,因为这是消息类型而不是不同的资源,而 :id 是单一资源。
解决方法:更改存档路由的顺序如下:
在应用程序中-routing.module.ts
{ path: 'messages', loadChildren: './pages/message/message.module#MessagePageModule' }
留言中-routing.module.ts
const messageRoutes: Routes = [
{
path: '',
children: [
{
path: 'archived',
component: ArchivedComponent
},
{
path: ':id',
component: MessageComponent
},
{
path: '',
component: MessageComponent,
pathMatch: 'full'
}
]
}
正如路线顺序所解释的那样here。这应该可以解决您的问题。