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?
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.
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.
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.
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.
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 }
];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With