Tag Archives: components

Changed best practice for Ionic 2 Component constructor

Up until now in my Ionic 2 applications when creating a component I have specified object types in the component’s constructor parameters e.g

 

import {NavController} from ‘ionic/ionic’;

@Page({
templateUrl: ‘build/pages/myPage/myPage.html’,
})

export class MyPage {
constructor(nav: NavController) {
this.nav = nav;
} {

}

i.e nav is of type NavController

Recently I noticed that in the Conference reference app being maintained by the Ionic team, the pages are now set up like this:

import {NavController} from 'ionic-angular';

@Page({
  templateUrl: 'build/pages/myPage/myPage.html'
})
export class MyPage {
  static get parameters() {
    return [[NavController]];
  }

  constructor(nav) {
    this.nav = nav;
  }
  
  ....
}

So nav is passed into the constructor and this magical looking get parameters method returns the NavController type.  I’m not exactly sure why this change has been made – presumably this is now considered Angular 2 best practice for component constructors?

 


I’m a mobile applications developer based in the UK, concentrating primarily on hybrid application development with Ionic and Ionic 2 but also with native development skills. Please visit www.crossplatformsolutions.co.uk for more information about me and how I may be able to help you with mobile application development, particularly with Ionic 2 but also with other mobile frameworks and technologies.  Thanks for visiting.