'How to refresh the page in angular with routerlink and id
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
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
