Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 71530

Why do people initialize data in the constructor?

$
0
0

@dev-gilbert wrote:

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?

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 71530

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>