I am currently exploring a project built with Ionic 2, Angular 2, and Typescript, and I find myself puzzled by the way member properties are being set. In the code snippet below, I noticed that Angular automatically injects dependencies into the constructor arguments. For instance, the 'nav' property is explicitly assigned to the value passed in through the constructor. On the other hand, when it comes to Firebase auth, we can access it within the 'login' method using 'this.auth', even though we haven't explicitly set it as a member variable. Why does this work for 'auth' but not for 'nav'? And what is the role of the 'private' keyword in the constructor parameters? Is it necessary to declare it both in the parameter list and in the property definition within the class? Does it automatically bind constructor variables to class properties if they share the same name?
export class LoginPage {
nav:NavController;
private auth: FirebaseAuth;
constructor(nav:NavController, private auth: FirebaseAuth) {
this.nav = nav; // Setting this is crucial for the setRoot function in login to work
//this.auth = auth; // How come we don't need to set this property here yet it's accessible in the login method?
}
login() {
this.auth.login().then(authData =>{
this.nav.setRoot(TabsPage);
});
}
}