On the page you will receive the parameters both by routerLink
how much for navigate
can make a logic to see where it came from.
Ex:
Using the routerLink
you pass the parameter this way:
idPagina1 = this.route.snapshot.params['id'];
Using the navigate
you get inside the ngOnInit
thus:
ngOnInit(){
this.route.queryParams.subscribe(
(queryParams: any) => {
this.idPagina2 = queryParams['id'];
});
}
Therefore, you can use a global variable where if the parameter comes from page 1 the idPagina1 will be different from null
, or if it comes from page 2 the idPagina2 will be different from null
. So you can check the parameter this way:
id: number;
ngOnInit(){
if(this.idPagina1 != null){
id = this.idPagina1;
}else{
this.route.queryParams.subscribe(
(queryParams: any) => {
this.idPagina2 = queryParams['id'];
id = this.idPagina2;
});
}
}
I don’t know if you’re using parameters, but I believe you’re trying to come up with different parameters from different to access a common page. Is that it? In case I think it helps you.
you are using this.route.navigate(['/route'])?
– Willian
got confused your question. Where are you running the
routerLink
? is in the same template as thengOnInit
? Where are you running thethis.router.navigate
? Within thengOnInit
? Questions related to the life cycle is good to take a look at this page: Angular Life Cycle Guide– Gaspar
I’ll try to simplify... I have a page that has the http GET method in ngOnInit. I have two other pages that will somehow redirect me to the front page. One will use the routerLink directive and the other will use this.route.navigate. The functions inside the ngOnInit of the homepage are executed when I access the page through the routerLink, but not by this.route.navigate
– Leonardo Vinicius
unintentional never had this problem posed to a code that went wrong
– Willian
Actually when you drive with the
this.router.navigate()
orrouterLink=[]
thengOnInit
component. Take a look at the routes that are correct for the proper component to run and make sure that the route that called the component bynavigate
is the same when called byrouterLink
– Gaspar
Have you considered using the router’s Resolve? https://angular.io/api/router/Resolve. You could place the request inside the Route Resolve, so before the route is called the request must have ended. Unless it is required to perform within ngOnInit(), which I do not see sense.
– Anderson Koester