typescript 如果您还使用 children 参数,请使用 redirectTo,这可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39290168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use redirectTo if you using also children parameter, is this possible?
提问by Vassilis Pits
As you probably already now in the latest @angular/router 3.0.0-rc.1
you are not allowed to user redirectTo
parameter if you also use the children
parameter.
由于您现在可能已经在最新版本@angular/router 3.0.0-rc.1
中,redirectTo
如果您还使用该children
参数,则不允许使用 user参数。
But for sure in some cases this is something that you need like in my case. For example what I want is to redirect to first child all requests to the parent router.
但可以肯定的是,在某些情况下,这是您需要的,就像我的情况一样。例如,我想要的是将所有请求重定向到第一个子级到父级路由器。
Here's my router:
这是我的路由器:
{
path: 'project/:id',
component: ProjectComponent,
children: [
{
path: 'properties',
component: ProjectPropertiesComponent
},
{
path: 'stats',
component: ProjectStatsComponent
}
]
}
I want everyone that goes to the project/:id
route to get redirected to the first child (properties
) in my case.
在我的情况下,我希望所有进入project/:id
路线的人都被重定向到第一个孩子 ( properties
)。
Is this possible somehow?
这有可能吗?
If I try like this:
如果我这样尝试:
{
path: 'project/:id',
component: ProjectComponent,
redirectTo: 'properties',
children: [
{
path: 'properties',
component: ProjectPropertiesComponent,
},
{
path: 'stats',
component: ProjectStatsComponent
}
]
}
I'm getting this error of course:
我当然收到这个错误:
EXCEPTION: Error: Invalid configuration of route 'project/:id': redirectTo and children cannot be used together
例外:错误:路由“project/:id”的无效配置:redirectTo 和 children 不能一起使用
回答by j2L4e
An empty child route should do the job:
一个空的子路由应该可以完成这项工作:
{
path: 'project/:id',
component: ProjectComponent,
children: [
{
path: '',
redirectTo: 'properties',
pathMatch: 'full'
},
{
path: 'properties',
component: ProjectPropertiesComponent,
},
{
path: 'stats',
component: ProjectStatsComponent
}
]
}
回答by walter
j2L4e answer is correct, but I was also forced to set the 'pathMatch' type as 'prefix' ..
j2L4e 答案是正确的,但我也被迫将 'pathMatch' 类型设置为 'prefix' ..
pathMatch: 'prefix'
路径匹配:'前缀'
{
path: 'project/:id',
component: ProjectComponent,
children: [
{
path: '',
redirectTo: 'properties',
pathMatch: 'prefix'
},
{
path: 'properties',
component: ProjectPropertiesComponent,
},
{
path: 'stats',
component: ProjectStatsComponent
}
]
}