Doubt routerLink and route.navigate(<route I want>)?

Asked

Viewed 505 times

-1

'Cause when I use the routerLink the destination tab executes the commands inside the ngOnInit() and when I use the this.route.navigate([<rota de acesso>]) is not executed?

I wonder if you have this.route.navigate() when running requires the target page to execute the commands within the ngOnInit().

  • you are using this.route.navigate(['/route'])?

  • got confused your question. Where are you running the routerLink? is in the same template as the ngOnInit? Where are you running the this.router.navigate? Within the ngOnInit? Questions related to the life cycle is good to take a look at this page: Angular Life Cycle Guide

  • 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

  • unintentional never had this problem posed to a code that went wrong

  • Actually when you drive with the this.router.navigate() or routerLink=[] the ngOnInit 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 by navigate is the same when called by routerLink

  • 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.

Show 1 more comment

1 answer

0

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.

  • Actually it wouldn’t be like implementing the route parameters his question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.