0

I have an angular application In that I have some navigation of routerlinks

My requirement is if the user page is already open with some Id ,and if the condition satisfies in then it has to navigate to that particular ID user page from the existing user page with ID.

.component.ts

  if(userID == 0){
             this.router.navigate(['./user/' + userID]);
           }
           else{
            this.router.navigate(['./details']);
            
           }

From the above code if I am already in the user page with id then condition satisfies in if condition then we have to navigate to that particular user page with that ID.

Can anyone help me on the same

user1
  • 63
  • 3
  • 11

1 Answers1

0

In order to refresh the current route, in your route declaration you have to do:

RouterModule.forRoot(appRoutes, {
  // ..
  onSameUrlNavigation: 'reload',
  // ..
})

Instead, to navigate to another route with params you have to:

1 - Redefine your route

{path: 'user/:userID', component: MyComponent }

2 - Navigate to page

this.router.navigate(["user", userID]);

3 - Get the param from your page

import { ActivatedRoute } from '@angular/router';

private userID: string;

constructor(private readonly activatedRoute: ActivatedRoute) {}

ngOnInit() {
    this.activatedRoute.params.subscribe((params: Params) => this.userID = params['userID']);
}

For more information see Angular router and ActivatedRoute

Shifenis
  • 1,056
  • 11
  • 22
  • Thanks @Shifenis, but I want to go to the particular page not reloading could you please edit the above on based on requirement – user1 Mar 10 '22 at 11:33
  • Sorry, but I don't get your point: do you want to refresh the page or navigate to another one? – Shifenis Mar 10 '22 at 13:06
  • Navigate to another one with ID – user1 Mar 10 '22 at 13:18
  • I want to navigate to the page as in the address bar link in browser, because the if condition satisfies then the address bar link is changing with the updated id but the page is not navigating, could you help me on the same – user1 Mar 10 '22 at 15:51