The Angular router allows us to navigate between the routes using either RouterLink directive or imperatively using router.navigate or router.navigatebyUrl method of the router service.
In this tutorial, we look at these methods more closely.
The Angular routes resemble directory-like tree structures.
Hence, We can use directory-like syntaxes like add / (root node), ./(current node), or ../(Parent node) in the link parameters array
The First segment of the link parameters array can be prepended with “/“, “./“, or “../“
If the First segment of the route starts with “/“, then the path is considered to be an Absolute path
If the First segment begins with “./” or it does not begin with a slash, then the path is considered to be the relative path.
And if the First segment begins with “. ./“, then the path is relative to the parent route. (one level up)
As mentioned earlier navigate method always uses the absolute path. To make Navigate method work with a relative path, we must let know the router where are we in the route tree.
This is done by setting the relativeTo Property to the ActivatedRoute as shown below
this._router.navigate(['detail'], { queryParams: { pageNum: this.pageNum + 1 }, relativeTo: this._Activatedroute } );
If you were using a RouterLink to navigate instead of the Router service, you’d use the same link parameters array, but you wouldn’t provide the object with the relativeTo property. The ActivatedRoute is implicit in a RouterLink directive.
It is recommended to use the Relative path. Using an absolute path breaks our code if the parent URL structure changes. The relative path will not change even if the parent path changes
To go to the parent route
<li><a [routerLink]="['../']">Back</a></li>
To go to the Sibling route
<li><a [routerLink]="['../<sibling>']">Goto sibling</a></li>
To go to the child route
<li><a [routerLink]="['<Child>']">Goto Child</a></li>
You can provide the extra options to the RouterLink directive, similar to the NavigationExtras. The following options are supported
QueryParams: Params
<a [routerLink]="['product']" [queryParams]="{ page:2}" }>Page 2</a>
preserveQueryParams: boolean
<a [routerLink]="['product']" { preserveQueryParams: "true" }">Page 2</a>
queryParamsHandling : QueryParamsHandling
<a [routerLink]="['product']" { queryParams: { page: 2 }, queryParamsHandling: "merge" }">Page 2</a>
<a [routerLink]="['product']" { fragment: 'top' }">Page 2</a>
<a [routerLink]="['product']" { preserveFragment: true }">Page 2</a>
<a [routerLink]="['product']" { skipLocationChange: true">Page 2</a>
<a [routerLink]="['product']" { replaceUrl: true">Page 2</a>
In this tutorial, we looked at various ways you can navigate between Angular routes, using the Angular router. We learn how to navigate using either RouterLink directive or using router.navigate or router.navigatebyUrl method provided by the router service. We learned how to set up relative and absolute path routing. Finally, we looked at the various options that are provided by these navigation methods.