0

I have two components: Component 1 and Component 2

Component 2 can be accessed via Component 1, from a button:

<ion-button [routerLink]="['/urlofComponent1'] [color]="'white'">Request </ion-button>

I have set a function in Component 2, to redirect user to 'Component 1' on page refresh, via:

this.router.navigate(['/urlofComponent1']);

When component 2 is already opened and page is refreshed, user is redirected to Component 1.

When I access Component 2 now, via the button, ngOnit() of Component 2 is not called.

Any suggestions?

Sarah Mandana
  • 1,474
  • 17
  • 43

3 Answers3

0

There is no lifecycle hook that fit your needs. you should write your own hook. Like what ionic team built ionViewDidEnter:

Runs when the page has fully entered and is now the active page. This event will fire, whether it was the first load or a cached page.

Here are some example codes: Event when component becomes visible

Goodluck

0

In case you are using

this.router.navigate(['/urlofComponent1']);

in constructor it can ignore ngOnInit because you are redirecting to another component before this one is initialized. You can use it at the end of your ngOnInit

ngOnInit(){

  // your code

  this.router.navigate(['/urlofComponent1']);
}
0

Its true because [ngOnInit] defined Doc Angular: Initialize the directive/component after Angular first displays the data-bound properties and sets the directive/component's input propertie and Called once, after the first ngOnChanges(). You can read https://angular.io/guide/lifecycle-hooks here then you will understand deep lifecle-hook. Try the above one if you didn't understand let me know i will edit it.

Kiên Lê
  • 23
  • 1
  • 3