I have an Ionic 4 application with Angular 7 development, the event ionViewDidEnter works if I first enter on the page, but if I move to another page and go back this event is not firing.
I change to ngOnDestroy from Angular and this works when the page is first loaded but when I go to the page and go back and leave this not working too.
My real problem that I have a login page and a home panel page, when I start the application my ngOnInit from my app.component.ts he verifies if the user is logged if is he redirect the user to panel home page that use this code
this.router.navigate(['painel/home']);
When this happens this event ionViewDidEnter and ionViewWillEnter work when this page appears, but when I click my button logout, my application clear the cache and change the navigation to this page auth.component.ts this is the code from logout
public logout(refresh?: boolean): void {
this.tokenStorage.clear();
if (refresh) {
this.router.navigate(['auth']);
}
}
Simple, but when I make a login again this events ionViewDidEnter and ionViewWillEnter don't fire and I want this to fire every time the user enters in panel home page, this is my code to log in application
async authenticationLogin() {
const loading = await this.loadingController.create({
message: 'Autenticando',
});
loading.present();
const userModel: any = {
afiliate_code: this.authForm.value.afiliate_code,
device_uuid: this.device.uuid,
};
let companyCode: string = this.authForm.value.company_code;
companyCode = companyCode.toUpperCase();
this.auth.login(userModel, companyCode, this.uuidValidation).pipe(
catchError(
(error: any): Observable<any> => {
loading.dismiss();
return this.utilService.errorHandler(error);
}))
.subscribe( async response => {
if (response.responseData.success === 1) {
if (this.authForm.value.checkauth_param) {
this.authParam = {
afiliate_code: userModel.afiliate_code,
companyCodeData: companyCode,
};
this.storageService.saveKeyStorage('param', this.authParam);
} else {
if (this.storageService.getKeyParamData('param')) {
this.storageService.removeKeyStorage('param');
}
}
this.submitted = false;
setTimeout(() => {
this.authForm.reset();
}, 2000);
this.router.navigate(['painel/home']);
} else if (response.responseData.success === 2) {
this.utilService.errorMsgAlert(response.responseData.message);
} else if (response.responseData.success === 3) {
const alert = await this.alertController.create({
header: 'Atenção!',
message: 'Você ainda não possui um dispositivo vinculado, deseja vincular este dispositivo ao seu usuário?',
buttons: [
{
text: 'Não',
role: 'cancel',
cssClass: 'secondary'
}, {
text: 'Sim',
handler: () => {
this.uuidValidation = true;
this.authenticationLogin();
}
}
]
});
await alert.present();
} else {
this.utilService.errorMsgAlert('Não foi possível fazer autenticação');
}
loading.dismiss();
});
}
That is working fine, what I'm doing wrong to not fire these events?
I solved my problem with this, I was using this code from Angular 7 core to change my pages
// import { Router } from '@angular/router';
this.router.navigate(['painel/home']);
But when I change to the Ionic 4 core
// import { NavController } from '@ionic/angular';
this.navController.navigateBack(['painel/home']);
Now ionViewDidEnter is fired every time, I read this article and solvend my problem.
Thank you all
https://medium.com/@paulstelzer/ionic-4-and-the-lifecycle-hooks-4fe9eabb2864