NgOnInit does not works in tab pages second time

There are some topics about this problem but they are outdated. I use Ionic 4.1.1.

When i navigate between tabs, NgOnInit does not fire second time. It only works once. So if i change a data in tab 2, tab 1 can’t get it. For example a tab has no any event like ionSelect or ionChange. Because there is no Ion-Tab anymore. Ion-Tabs component has ionTabsDidChange but it’s useless in tab pages as i understand.

How can i trigger NgOnInit second time or call a function in a tab page when tab change?

Thanks in advance.

1 Like

I found a solution but i’m not sure if it’s a proper solution: ionViewWillEnter()

I removed ngOnInit and added -ionic 4 life cycle hook- ionViewWillEnter. It works well on every click. But is this an appropriate solution?

Btw, i can’t find official docs about ionic lifecycle hooks. :confused:

Hey so can I just use this in my component ts file without any imports? I can’t seem to find any documentation for ionic lifecycle hooks. :slight_smile:

Yes. No need OnInit etc. Just ionViewWillEnter() works well.

57

1 Like

Really depends on ur use case. For me, i use ngOnInit to initialize my data once when user access each tab. If ur data is being heavily re-initialize by putting ur method in ionViewDidEnter, u should consider putting it in ngOnInit instead. It can cause performance issue to keep running those methods in ionViewDidEnter if it is only meant to be initialize once.

Hope this clarifies. :slight_smile:

I was also having the same issue you were having. And thanks to you. Your question in forum helped me to find life cycle methods of Ionic.
Here is a documentation about Ionic and Angular life cycle hooks