Angular2 routing:redirectTo相对url

时间:2017-01-05 15:57:28

标签: angular angular2-routing

所有内容都在标题中:您是否可以使用redirectTo属性重定向到相对网址?

我的(简化)路由如下:

{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的相对路由?

编辑整条路线:

app.module

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

logged.module

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

admin.module

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

main.module

{ 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' },
] },

1 个答案:

答案 0 :(得分:1)

您只需要将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'}