bryomckim bryomckim - 8 days ago 6
Javascript Question

Angular 2 navigate to root route "/"

I am trying to navigate to home route from a child route but nothing happens.

I.e. the current route is

/projects


Neither
this.router.navigate(['/'])
nor
routerLink="/"
works.

My routerConfig looks like this

const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: '/projects',
component: ProjectsComponent
}
]


How do I go about doing this?

Answer

Try adding this redirectTo to your routes,

const routes: Routes = [
    { path: '', redirectTo: 'home', pathMatch: 'full' },
    { path: '/home', component: HomeComponent },
    { path: '/projects', component: ProjectsComponent }
    ];
Comments