ionic-framework: [v4] Bug: ionViewWill/DidEnter not firing second time (similar to #12805)
Bug Report
Ionic Info
Run ionic info
from a terminal/cmd prompt and paste the output below.
Ionic:
ionic (Ionic CLI) : 4.2.1 (/Users/clarako/.npm-global/lib/node_modules/ionic)
Ionic Framework : @ionic/angular 4.0.0-beta.13
@angular-devkit/build-angular : 0.8.6
@angular-devkit/schematics : 0.8.6
@angular/cli : 6.2.6
@ionic/angular-toolkit : 1.0.0
Cordova:
cordova (Cordova CLI) : 8.0.0
Cordova Platforms : ios 4.5.5
Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 2.2.0, (and 7 other plugins)
System:
ios-deploy : 1.9.2
NodeJS : v8.12.0 (/usr/local/bin/node)
npm : 6.4.1
OS : macOS High Sierra
Xcode : Xcode 9.4.1 Build version 9F2000
Describe the Bug Neither ionViewWillEnter or ionViewDidEnter is being called when returning to a page the second time using router.navigate or router.navigateByUrl.
Steps to Reproduce Steps to reproduce the behavior:
-
Start with clean Ionic4 tabs starter template ionic start myApp tabs (yes to Ionic4 Beta)
-
For each of the 3 tabs (home,about,contact) write ionViewWillEnter and ionViewDidEnter to console Example:
import { Component, OnInit } from '@angular/core'
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss']
})
export class HomePage implements OnInit {
constructor() {
console.log('HomePage')
}
ngOnInit() {
console.log('HomePage: ngOnInit')
}
ionViewWillEnter(){
console.log('HomePage: ionViewWillEnter')
}
ionViewDidEnter(){
console.log('HomePage: ionViewDidEnter')
}
}
- ionic serve (watch console log)
- Notice when clicking to a tab the 2nd time does not call ionViewWill/DidEnter
- Same problem with backend routing using router.navigate or router navigateByUrl (ionViewWill/DidEnter is not called if it is not the first time the page is loaded)
Related Code If you are able to illustrate the bug with an example, please provide a sample application via an online code collaborator such as StackBlitz, or GitHub.
Expected Behavior I expect to be able to get ionViewWill/DidEnter every time a tab is clicked and when using router.navigate/navigateByUrl
Additional Context List any other information that is relevant to your issue. Stack traces, related issues, suggestions on how to fix, Stack Overflow links, forum links, screenshots, OS if applicable, etc.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 4
- Comments: 19 (7 by maintainers)
@hiepxanh What life cycle event in Angular is like ionViewWill/Did Enter?
Thanks @olivermuc for providing the workaround.
I tried to use it but ran into something. Sorry for the basic question.
I get this error:
[ng] ERROR in src/app/home/home.page.ts(13,11): error TS2552: Cannot find name 'filter'. Did you mean 'File'?
I also would consider this a bug, as
ionViewWill/Did Enter
is an ionic feature to catch the moment a page, modal, popup, or tab etc entered (repeatedly regardless of component lifecycle). As opposed to the angular lifecycle event such as ngOnInit which really only is triggered once when the component is created. Both events make sense and have their purpose.I need to know when someone switches from tab to tab so
ionViewWill/Did Enter
is required to fire each time.Example: On tapping a tab I would like to reset the individual tab’s badge to 0;
Workaround:
EDIT: added example EDIT2: added workaround
I wrote an article on Medium about this topic -> https://medium.com/@paulstelzer/ionic-4-and-the-lifecycle-hooks-4fe9eabb2864
At the moment (beta.15) ion-tabs stay in the stack, so after initialize a tab page, it will keep online and will not be destroyed if you switch between tabs. Maybe the ionic team changes this behaviour in future (they redesigned tabs in beta.15, so removing a tab after switching will be maybe added later).
So at the moment, you are right switching back to an tab you opened previous, will not trigger any lifecycle hooks. For me it’s not a bug (because the components aren’t destroyed or will be entered because they are still alive all time). For me adding such a behaviour would be a nice feature like
@angular/material
did at their tabs.none of the life cycles events are triggering the second time you visit the tabs/other pages