Even in the Ionic docs I see examples where they initialize data in the constructor, like so:
export class AppComponent {
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
private authService: AuthenticationService,
private router: Router
) {
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.authService.authenticationState.subscribe(state => {
console.log("Auth state: " + state);
if (state) {
this.router.navigate(["home"]);
} else {
this.router.navigate(["login"]);
}
});
});
}
}
It is my understanding that it’s a good practice to initialize data in the ngOnInit
lifecycle method instead, but still I see time and time again that in Ionic apps people initialize data in the constructor.
Why in the example above the author would choose to use the authService in the constructor? I think it would be better in the ngOnInit
method no?