0

In my angular application I have some navigation pages with router.

And my requirement is when it is navigating it has to reload the page.

.component.ts

if(ID == 0){
             this.router.navigate(['./Profilepage/' + ID]);
          }
           else{
            this.router.navigate(['./dashboard']);
            
           }


From the above code if I am already there in Profilepage with some ID, and if the condition in if condition satisfies then it has to go that particular Profilepage with that ID.

But for me it is not showing that redirecting, and is happening only after refreshing the page.

Can anyone help me how I can go to the particular page with the ID (if condition satisfies).

user1
  • 63
  • 3
  • 11

2 Answers2

0

your question is not quite clear but I think I get it:

you need to "listen" to the route, so you can detect every change in route and navigate according to that change. try something like this:

import { ActivatedRoute, Params } from '@angular/router';
export class SomeClass implements OnInit {

paramFromRoute;

constructor(private route: ActivatedRoute) { }

ngOnInit() {
this.paramFromRoute = this.route.snapshot.params['paramName']; // this one is 
// required for getting it first time


this.route.params.subscribe((params:Params)=>{
this.paramFromRoute =  params['paramName'] // whenever route is changed, this 
function will triggered.
 });
// for queryParams you can subscribe to this.route.queryParams
}   
}
Eli Porush
  • 1,339
  • 1
  • 7
  • 18
0

My guess is you want to update the view with the new profile based on the id in the router params. That means you'll have to listen for changes in the activated route and a page reload isn't required at all.

See this page for an example: https://angular-training-guide.rangle.io/routing/routeparams

constructor(private activatedRoute: ActivatedRoute) {}

ngOnInit() {
  this.activatedRoute.params
    .pipe(
        map(params => params.id)
    )
    .subscribe(profileId => {
        this.profileId = +profileId; // (+) converts string 'id' to a number

       // Update view or profile object to load the details here.
    });
}
SeppeDev
  • 2,252
  • 2
  • 13
  • 26
  • Thanks@SeppeDev, could you please edit the above code with my requrement with the above on eso that it will be helpfulfor me, because I have not written the above in after ngOnInit, can edit the above one – user1 Mar 09 '22 at 15:17
  • @user1 I'm not really sure what you mean but could you provide a minimal example with https://stackblitz.com/edit/angular so I can better understand what your problem is. – SeppeDev Mar 09 '22 at 16:14