Instead of using <a href="/my-path/{{my_param}}"></a>
in my template, I am willing to use a function with paramters to redirect me to a page.
So, this is how I built my function in my .ts file:
redirectToChat(my_param){
this.router.navigate(['./my-path/' + my_param ]);
}
And how I called it in my template:
<div (click)="redirectToChat(my_param)">
...
</div>
It changes the URL, but not rendering the component. I imported this in my component:
import { Router, ActivatedRoute, ParamMap } from '@angular/router';
And I used it in my constructor: private router: Router
Can you help me to render corrctly the component? How else can I redirect the user to the page using a function?
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.
Generate an application with routing enabledlink The following command uses the Angular CLI to generate a basic Angular application with an application routing module, called AppRoutingModule , which is an NgModule where you can configure your routes.
Steps to get current route URL in Angular. Import Router,NavigationEnd from '@angular/router' and inject in the constructor. Subscribe to the NavigationEnd event of the router. Get the current route url by accessing NavigationEnd's url property.
You are mixing navigate (to component) with navigate by url. Have you tried navigateByUrl?
this.router.navigateByUrl('./my-path/' + my_param);
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