Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 routing redirect to with child routes

I am a newbie in Angular 2. I want to create isolated modules for every part of my app. For example I created the AuthModule with default component - AuthComponent which contain a router-outlet for his child components (SignIn or SignUp). So I want to realise the following scenario:

  1. When navigate to / - root off app - redirect to /auth
  2. After redirect to /auth - load AuthComponent with router outlet
  3. After AppComponent loaded - load default sign-in component via redirecting to /auth/sign-in

But when I going to localhost/ I get redirect to /auth what I want, but the next redirect to sign-in doesn't appear.

My code: app.routing

const routes: Routes = [   {       path: '',        redirectTo: '/auth',        pathMatch: 'full'   } ];  export const appRouting: ModuleWithProviders = RouterModule.forRoot(routes); 

auth.routing

const routes: Routes = [   {     path: 'auth',     component: AuthComponent,     children: [       {          path: '',           redirectTo: 'sign-in',           pathMatch: 'full'       },       {          path: 'sign-in',           component: SignInComponent       }     ]   },  ];  export const authRouting: ModuleWithProviders = RouterModule.forChild(routes); 

auth.component.html

<div class="container">     <h1>Auth component</h1>     <router-outlet></router-outlet> </div> 

Result:

code

result

Environment @angular/cli: 1.0.0-rc.2 node: 7.7.1 os: win32 x64

like image 603
Timofey Orischenko Avatar asked Mar 18 '17 13:03

Timofey Orischenko


People also ask

Where would you define child URL routes?

In Angular, the router lets you add child routes using the children property inside the routing module. Here you can see that the routing module has been updated with the child route and added to the array of components so we do not need to import all of them wherever we go.

What is Routeroutlet in Angular?

The Router-Outlet is a directive that's available from the router library where the Router inserts the component that gets matched based on the current browser's URL. You can add multiple outlets in your Angular application which enables you to implement advanced routing scenarios.

What is ActivatedRoute in Angular?

What is an activated route? The Angular Docs define the ActivatedRoute as. A service that is provided to each route component that contains route specific information such as route parameters, static data, resolve data, global query params and the global fragment.

What is redirecting routes in Angular?

When the application start, it navigates to the empty route by default. We can configure the router to redirect to a named route by default. So, a redirect route translates the initial relative URL (”) to the desired default path.


2 Answers

I have been the same problem. It seems an Angular tricks: If you remove leading slash in 'redirectTo' field, your application will be redirected successfully to auth/sign-in.

Use this in app.routing:

const routes: Routes = [       {path: '', redirectTo: 'auth', pathMatch: 'full'},   ]; 

‘redirectTo’ value starts with a ‘/’ = absolute path

‘redirectTo’ value starts without a ‘/’ = relative path

Read more about it: https://vsavkin.com/angular-router-understanding-redirects-2826177761fc

P.S My opinion that your structure more correctly then YounesM's one. Parent module can't keep children routes: "app" module don't know that "auth" module have children module "sign-in".

like image 145
Dmitriy Ivanko Avatar answered Sep 26 '22 20:09

Dmitriy Ivanko


On auth.routes

const routes: Routes = [   {     path: "auth",     redirectTo: "auth/sign-in"   },   {     path: "auth",     component: AuthComponent,     children: [{ path: "sign-in", component: SignInComponent }]   } ]; 
like image 28
Chema Avatar answered Sep 24 '22 20:09

Chema