I am having a tough time doing Ionic 3 to 4 migration. The Official migration guide asks us to use "the proper Angular alternatives". Can anyone tell me the equivalent Angular lifecycle-hooks replacements for the following Ionic 3 lifecycle-hooks.
Please help me match the above with
Edit : I have indeed gone through Ionic 4 router-outlet docs.
ionViewDidLoad => ngOnInit()
ionViewWillUnload => ngOnDestroy()
From Angular Docs :
ngOnInit()
Initialize the directive/component after Angular first displays the data-bound properties and sets the directive/component's input properties.ngOnDestroy()
Cleanup just before Angular destroys the directive/component. Unsubscribe Observables and detach event handlers to avoid memory leaks.
For ionViewCanEnter() and ionViewCanLeave() you have to use Router Guards as suggested in the docs. For eg. If you want to guard a route against an unauthenticated user you have to first create service file to check for authentication
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
if (isAuthenticated) {
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
this.router.navigate(['/login']);
return false;
}
}
and then in your route path use the service in canActivate property:
{path:'home',component:HomeComponent,canActivate:[AuthGuard]}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With