Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 539
koech Avatar asked Nov 24 '16 15:11

koech


People also ask

What does router navigate do?

In Angular, RouterLink is a directive for navigating to a different route declaratively. Router. navigate and Router. navigateByURL are two methods available to the Router class to navigate imperatively in your component classes.

How do you navigate to a parent route from a child route?

Directly using the routerLink directive in case of linkage of child to parent. Using Route class in case of navigation to happen on a triggered event.

Which of the following paths specifies the root route?

Default is "/" (the root path). The path-matching strategy, one of 'prefix' or 'full'. Default is 'prefix'. By default, the router checks URL elements from the left to see if the URL matches a given path and stops when there is a config match.

What is the difference between navigate and navigateByUrl in Angular?

It has two methods, navigate and navigateByUrl , that navigate the routes. They are similar; the only difference is that the navigate method takes an array that joins together and works as the URL, and the navigateByUrl method takes an absolute path.


1 Answers

Try adding a redirectTo route to your routes config:

const routes: Routes = [
    { path: '', redirectTo: 'home', pathMatch: 'full' },
    { path: 'home', component: HomeComponent },
    { path: 'projects', component: ProjectsComponent }
];
like image 137
wuno Avatar answered Oct 04 '22 04:10

wuno