Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
365 views
in Technique[技术] by (71.8m points)

angular - How to make optimal routing for ionic applications ? What life cycle methods should I choose?

In Ionic, pages are cached, and because of this, it is not always good to use angular lifecycle hooks
(ngOnInit, ngOnDestoy, ...). They just won't work, because the page is still on the stack.


With the help of Google and experiments, I learned 2 solutions (+ a little experimentation...).

  • Option A => "ng-hooks + ion-navigation"
    You can use angular lifecycle hooks (ngOnInit, ngOnDestroy, ...),
    but you need to navigate using navController.navigateRoot(...)
    (why this method solves the problem-I do not know, it is magic)

  • Option B => "ng-navigation + ion-hooks"
    You can apply angular routing using router.navigate(...),
    but then you will need to use Ionic lifecycle hooks
    (for example, ionViewDidEnter, ionViewDidLeave, ...)

This is certainly good, but I want more angular logic and less ionic logic.
question #1 => Which option do you use and why ?


And yet it seems to be different pages are cached in different ways.
One page when routed will simply change its position in the stack,
and the other will be removed from the stack (for some reason)
This is where the complete misunderstanding begins for me.

question #2 => How does it work (in simple words) ?


P.S.: thank you in advance for your answer, I am very interested to know )

question from:https://stackoverflow.com/questions/65927878/how-to-make-optimal-routing-for-ionic-applications-what-life-cycle-methods-sho

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Depending on the ionic version.

Version 3 uses nav controller to push pages.

Version 4 and above uses the same router as angular.

But both uses the ionic page lifecycle hooks.

  • ionViewDidEnter

https://ionicframework.com/docs/angular/lifecycle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...