2017-01-05 18 views
1

Alles ist im Titel: können Sie die redirectTo Eigenschaft verwenden, um zu einer relativen URL umzuleiten?Angular2 Routing: redirectTo eine relative URL

Mein (vereinfacht) Routing ist wie folgt:

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

Mein Routing gut funktioniert, mit der Ausnahme, dass, wenn ich URL/admin/ ein Fehler erreichen wollen, dass Armaturenbrett unbekannt auftritt sagen ist. Aber mit redirectTo: 'admin/dashboard' funktioniert das Routing.

Gibt es eine Möglichkeit, relative Routing mit rediretcTo zu verwenden?

EDIT Ganze Routen:

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

Antwort

0

Sie müssen nur pathMatch: 'full' auf Ihre Umleitung Route hinzufügen und es sollte funktionieren, wie Sie es das darauf abzielt.

{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'} 
+0

Ich habe schon versucht, und es hat nicht funktioniert. Offensichtlich muss die URL beim Hinzufügen von pathMatch dem Parameter vollständig entsprechen. Ich habe erwartet, ein 'Suffix 'zu haben, wie es ein Präfix gibt, aber leider ist es nicht der Fall. – trichetriche

+0

Würde es Ihnen etwas ausmachen, Ihr gesamtes Routenarray zu posten? Für mich funktioniert das so ... Aber vielleicht, weil du zwei Optionen für einen leeren Pfad auf derselben Ebene hast, versuche den 'admin' Pfad auf die selbe Ebene wie' login' zu setzen. Sie können Ihre Wachen auch dort in der Zukunft hinzufügen. –