Redirecting the Router to Another Route in Angular 2


When your application starts, it navigates to the empty route by default. We can configure the router to redirect to a named route by default:

export const routes: Routes = [
  { path: '', redirectTo: 'path-one', pathMatch: 'full' },
  { path: 'path-one', component: ComponentOne },
  { path: 'path-two', component: ComponentTwo }

The pathMatch property, which is required for redirects, tells the router how it should match the URL provided in order to redirect to the specified route. Since pathMatch: full is provided, the router will redirect to path-one if the entire URL matches the empty path ('').
When starting the application, it will now automatically navigate to the route for path-one.

Related Post

Latest Post

Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.