How to refresh the page in angular with routerlink and id

9.2k views Asked by At

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

1

There are 1 answers

4
Shifenis On

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