簡體   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