Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

router.navigate changes the URL, but is not rendering the component

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?

like image 729
Emanuela Colta Avatar asked Sep 12 '17 14:09

Emanuela Colta


People also ask

What does router navigate do in Angular?

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.

Which Angular package is used to route to URL?

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.

How do I find the URL of a Activated route?

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.


Video Answer


1 Answers

You are mixing navigate (to component) with navigate by url. Have you tried navigateByUrl?

this.router.navigateByUrl('./my-path/' + my_param);
like image 199
stiGVicious Avatar answered Sep 18 '22 04:09

stiGVicious