Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 routing redirect not working

I have a redirect defined in app-routing.module and have no ideas why is it not working. If I use a default path for ""(code that is currently commented out) instead of a redirect then it works fine.

@NgModule({
imports: [
    RouterModule.forRoot([
        {
            path: "actions",
            component: ActionsComponent
        },
        //{
        //    path: "",
        //    component: LoginComponent
        //}
        {
            path: "",
            redirectTo: "/login",
            pathMatch: "full"
        }
    ])
],
exports: [
    RouterModule
]

})

Any ideas? Code is in http://plnkr.co/edit/y970zDKDZRIZ5LQA8v1T?p=preview

like image 941
wm_ Avatar asked Oct 29 '22 17:10

wm_


1 Answers

In your reproduction you don't have really /login path because the second router-outlet don't know which child of the login route should be process.

In the answer of @Ha Hoang if you comment below line :

{ path: "", redirectTo: "login", pathMatch: "full" }

your app still work ! because (path: "") override in login-routing.module on line :

{ path: '', component: LoginComponent }

but I think the way of @Ha Hoan is not logical because the path="" should not be load LoginComponent it should redirect to /login url and then path="login" should load LoginComponent my suggestion is :

In app-routing.module: use /login/loginDetails instead of /login

{
    path: "",
    redirectTo: "/login/loginDetails",
    pathMatch: "full"
}

then your plunker work correct and the structure of the routing is better. http://plnkr.co/edit/YENJ4Wm26qMty5mOR1dA?p=preview

like image 176
Mohammad Hasani Avatar answered Nov 11 '22 13:11

Mohammad Hasani