当我更改页面时 ionViewDidLeave 和 ionViewDidEnter 不会触发

wav*_*rik 3 ionic-framework angular ionic4

我有一个使用 Angular 7 开发的 Ionic 4 应用程序,如果我第一次进入页面,则 ionViewDidEnter 事件会起作用,但如果我移动到另一个页面并返回,则该事件不会触发。

\n\n

我从 Angular 更改为 ngOnDestroy,这在页面首次加载时有效,但当我转到页面并返回时,它也不起作用。

\n\n

我真正的问题是,我有一个登录页面和一个主页面板页面,当我从 app.component.ts 启动应用程序时,他会验证用户是否已登录,是否将用户重定向到使用此代码的面板主页

\n\n
this.router.navigate(['painel/home']);\n
Run Code Online (Sandbox Code Playgroud)\n\n

发生这种情况时,此页面出现时,此事件ionViewDidEnter 和 ionViewWillEnter起作用,但是当我单击按钮注销时,我的应用程序会清除缓存并将导航更改到此页面 auth.component.ts 这是注销中的代码

\n\n
public logout(refresh?: boolean): void {\n    this.tokenStorage.clear();\n    if (refresh) {\n      this.router.navigate(['auth']);\n    }\n  }\n
Run Code Online (Sandbox Code Playgroud)\n\n

简单,但是当我再次登录时,此事件ionViewDidEnter 和 ionViewWillEnter不会触发,我希望每次用户进入面板主页时都会触发此事件,这是我登录应用程序的代码

\n\n
async authenticationLogin() {\n    const loading = await this.loadingController.create({\n      message: 'Autenticando',\n    });\n    loading.present();\n\n    const userModel: any = {\n      afiliate_code: this.authForm.value.afiliate_code,\n      device_uuid: this.device.uuid,\n    };\n    let companyCode: string = this.authForm.value.company_code;\n    companyCode = companyCode.toUpperCase();\n\n    this.auth.login(userModel, companyCode, this.uuidValidation).pipe(\n    catchError(\n      (error: any): Observable<any> => {\n        loading.dismiss();\n        return this.utilService.errorHandler(error);\n      }))\n    .subscribe( async response => {\n      if (response.responseData.success === 1) {\n        if (this.authForm.value.checkauth_param) {\n          this.authParam = {\n            afiliate_code: userModel.afiliate_code,\n            companyCodeData: companyCode,\n          };\n          this.storageService.saveKeyStorage('param', this.authParam);\n        } else {\n          if (this.storageService.getKeyParamData('param')) {\n            this.storageService.removeKeyStorage('param');\n          }\n        }\n        this.submitted = false;\n        setTimeout(() => {\n          this.authForm.reset();\n        }, 2000);\n        this.router.navigate(['painel/home']);\n      } else if (response.responseData.success === 2) {\n        this.utilService.errorMsgAlert(response.responseData.message);\n      } else if (response.responseData.success === 3) {\n        const alert = await this.alertController.create({\n          header: 'Aten\xc3\xa7\xc3\xa3o!',\n          message: 'Voc\xc3\xaa ainda n\xc3\xa3o possui um dispositivo vinculado, deseja vincular este dispositivo ao seu usu\xc3\xa1rio?',\n          buttons: [\n            {\n              text: 'N\xc3\xa3o',\n              role: 'cancel',\n              cssClass: 'secondary'\n            }, {\n              text: 'Sim',\n              handler: () => {\n                this.uuidValidation = true;\n                this.authenticationLogin();\n              }\n            }\n          ]\n        });\n        await alert.present();\n      } else {\n        this.utilService.errorMsgAlert('N\xc3\xa3o foi poss\xc3\xadvel fazer autentica\xc3\xa7\xc3\xa3o');\n      }\n      loading.dismiss();\n    });\n  }\n
Run Code Online (Sandbox Code Playgroud)\n\n

工作正常,我做错了什么而不触发这些事件?

\n

wav*_*rik 8

我用这个解决了我的问题,我使用 Angular 7 core 中的代码来更改我的页面

 // import { Router } from '@angular/router';
 this.router.navigate(['painel/home']);
Run Code Online (Sandbox Code Playgroud)

但是当我换成 Ionic 4 核心时

 // import { NavController } from '@ionic/angular';
 this.navController.navigateBack(['painel/home']);
Run Code Online (Sandbox Code Playgroud)

现在ionViewDidEnter每次都会被触发,我阅读了这篇文章并解决了我的问题。

谢谢你们

https://medium.com/@paulstelzer/ionic-4-and-the-lifecycle-hooks-4fe9eabb2864