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

0

When I use routerLink , does the target page run the commands within ngOnInit() and when I use this.route.navigate([<rota de acesso>]) does not execute?

I would like to know if you have this.route.navigate() at runtime require the landing page to execute the commands within ngOnInit() .

    
asked by anonymous 29.09.2018 / 23:11

1 answer

0

On the page you will receive the parameters both by routerLink and navigate you can make a logic to see where it came from.

Ex:

Using routerLink you pass the parameter this way:

idPagina1 = this.route.snapshot.params['id'];

Using navigate you get within ngOnInit this way:

ngOnInit(){
 this.route.queryParams.subscribe(
        (queryParams: any) => {
        this.idPagina2 = queryParams['id'];
 });
}

So you can use a global variable where if the parameter comes from page 1 the idPage1 will be different from null , or if it comes from page 2 the idPage2 will be different from null . So you can check the parameter like this:

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 do not know if you are using parameters, but I believe you are trying to come up with different parameters from different to to access a common page. It is? In case I think it helps you.

    
01.10.2018 / 23:30