Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 4 - Angular 6: How to control Ionic router history to stop cache a view component?

Maybe I am missing something, but I'm experiencing a problem with Ionic caching views previously visited, preventing the components from being re-initialized by Angular. The components are pulled from some cache and rendered as whatever data existed previously.

Example:

User A is logged into the application and starts on the 'Home' page with info relevant to user A. User A logs out and navigates to 'Log In'.

User B logs in from the same application and navigates to 'Home' page. Ionic see's that Home was previously visited (by user A) and instead of instantiating 'Home', the view is pulled from cache and displays everything that User A was seing.

I noticed ion-router-outlet adds page transitioning when navigation, and this transition swaps to the left, when the navigation goes "forward" and swipe right when the navigation goes "backwards". This data seems to read from the same history-source that stores cached views.

TL;DR How to control Ionic router history (url tree) from caching the views preventing angular to re-initialize the components?

like image 365
Bjarne Gerhardt-Pedersen Avatar asked Feb 03 '23 21:02

Bjarne Gerhardt-Pedersen


2 Answers

Had the similar issue 2 days ago. i am now using the ionic lifecycle hook ionViewWillEnter. and by forcing the logic in this hook i achieved my desired results. Let me know if this is helpful :)

like image 178
Muhammad Abdullah Shafiq Avatar answered Feb 06 '23 10:02

Muhammad Abdullah Shafiq


I solved my problem using Ionic's life cycle hooks to fire the behavior needed, since Ionic does not destroy og init Angular components from the stack if previously visited. This is a longer topic, and it seems some things in Ionic stack control is not 100% fixed yet. Here are the hooks I routinely implement now to make things work:

  1. ngOnInit
  2. ionViewWillEnter
  3. ionViewDidEnter
  4. ionViewWillLeave
  5. ionViewDidLeave
  6. ngOnDestroy
like image 41
Bjarne Gerhardt-Pedersen Avatar answered Feb 06 '23 11:02

Bjarne Gerhardt-Pedersen