Based on the conversation from the comments:
It may be the same from the Angular point of view, but Pages and Components have a different meaning in Ionic. In terms of Angular, both are just components, but in the context of Ionic, a Page is a component that will act as an entire view (it may have nested components); we see Ionic pages as a standalone concept. A component will be just part of a bigger component most of the time in Angular apps, so I guess that's the biggest difference with Pages.
About when using Angular's lifecycle hooks, I like to use them when working in nested components, but I prefer Ionic lifecycle hooks when working on pages. Mostly because things like ionViewWillEnter
doesn't make too much sense in the context of a simple component, where ngOnInit
does. That being said, I also used some Angular lifecycle hooks on Pages, like the ngOnDestroy
(I used it to remove all the subscriptions from a page when that page is going to be destroyed), but just like you said, ionViewWillUnload
seems to be the right way to do it if we want to use Ionic's lifecycle hooks.
I guess that most of the Ionic lifecycle hooks are more related to the way the user interacts with the page as a whole (will enter to a page, will leave from a page, can enter to a page, can leave from a page...) and Angular lifecycle hooks are more related to the different stages of the life of a single component (the inputs has been initialized, the change detector has checked if there were changes in this component, ...), which as you can see, may not be directly related to the user interaction at all, and usually are things that the user is not aware of.
I'm pretty sure there isn't a rule about which approach is better, but the most important thing is consistency. I think it make sense to use Ionic lifecycle hooks in the components that are Pages, and use Angular lifecycle hooks inside of nested components, but you can use a different approach, as long as you do it consistently in the entire app.
ionViewWillEnter
doesn't make too much sense inside of a simple component, wherengOnInit
does. I also use some Angular lifecycle hooks on Pages, like thengOnDestroy
(I use it to remove all the subscriptions from a page when that page is destroyed). I'm not sure if this is the best way to use them, but I guess it makes sense... – AirsickionViewWillLeave()
event for clearing subscriptions? Is that not good? @Airsick – EnlightenmentionViewWillLeave
you may clean up the subscriptions and the next time the user goes to that page (if it was cached, and thus, not created again) the code related to those subscriptions won't be executed. – AirsickionViewWillUnload - Runs when the page is about to be destroyed and have its elements removed.
.Can't we use this? @Airsick – Enlightenmentconstructor
? It depends on the place where we create the subscriptions no? I mean the clearance of subscriptions? If we create it inside theionViewDidEnter()
then it doesn't depend on the cache scenario no? @Airsick – EnlightenmentionViewWillUnload
seems like the right place to do the clean up (for some reason I was not aware of that hook). And also you're right about the cache scenario, if you use Ionic hooks to create the subscription, you can also use Ionic hooks to clean it up. It's all about in what particular time (hook) do you want to do something. – Airsick