Search code examples
angularangular2-router

Redirect a state to default substate with Router in Angular2


What's the correct way of having default substate in new Angular2 Router?

for example I want router

/user

redirects to

/user/profile

like a default substate of user state.


Solution

  • You can use redirectTo as shown below.

    When user tries to navigate to www.domain.com/user because of redirectTo, he will be redirected to www.domain.com/user/profile.

    import { Routes,RouterModule } from '@angular/router';
    
    let routes: Routes = [
      { path: '/user', redirectTo: '/user/profile', pathMatch: 'full'},
      { path: '/user/profile', component:somecomponent },  
    ];
    
    export const routing = RouterModule.forRoot(routes);