繁体   English   中英

Angular2 路由:重定向到相对 url

[英]Angular2 routing : redirectTo a relative url

一切都在标题中:您可以使用redirectTo属性重定向到相对 URL 吗?

我的(简化)路由如下:

{path: 'login', children: []},
{path: '', children: [ // made here for future guards
    {path: 'admin', children: [
        {path: 'dashboard', children: []}
        {path: '', redirectTo: 'dashboard'}
    ]}
]},
{path: '', redirectTo: 'login', pathMatch: 'full'}

我的路由运行良好,除了当我想访问URL/admin/出现错误,指出仪表板未知。 但是,使用redirectTo: 'admin/dashboard'路由工作。

那么有没有办法在rediretcTo使用相对路由?

编辑整个路线:

应用模块

{ path: 'login', component: LoginComponent, children: [] },
{ path: 'signup', component: SignupComponent, children: [] },
{ path: '', redirectTo: '/login', pathMatch: 'full' },
...loggedRoutes

记录模块

{ path: 'logged', component: LoggedComponent, children: [
{ path: 'profile', component: ProfileComponent, children: [] },
    ...adminRoutes,
    ...mainRoutes
] },

管理模块

{ path: 'admin', component: AdminComponent, children: [
    { path: 'dashboard', component: DashboardComponent, children: [] },
    { path: 'validation/:objectid', component: ValidationComponent, children: [] },
    { path: '', redirectTo: '/logged/admin/dashboard' },
] },

主模块

{ path: 'main', component: MainComponent, children: [
    { path: 'error', component: ErrorComponent, children: [] },
    { path: 'last', component: LastComponent, children: [] },
    { path: 'process', component: ProcessComponent, children: [] },
    { path: 'crt', component: CrtComponent, children: [] },
    { path: '', redirectTo: '/logged/main/error' },
] },

您只需要将pathMatch: 'full'添加到您的重定向路由中,它应该可以按照您的预期工作。

{path: 'login', children: []},
{path: '', children: [ // made here for future guards
    {path: 'admin', children: [
        {path: 'dashboard', children: []}
        {path: '', redirectTo: 'dashboard', pathMatch: 'full'} // <-- ADDED here
    ]}
]},
{path: '', redirectTo: 'login', pathMatch: 'full'}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM