angular - Do I need constructor body in Angular2? -
i couldn't find difference between:
constructor (private router: router) { }
and
router: router; constructor (private _router: router) { this.router = _router }
variable router
available in whole class , contains same data. what's difference between first , second syntax?
basically this:
constructor (private router: router) { }
is short form of this:
private router: router; constructor (_router: router) { this.router = _router }
personally use first format in projects because makes files shorter, , easier read.
if question block inside of constructor, answer - no. if using short form showed before, there no need put in constructor. needed init stuff can put in ngoninit
function.
short example:
@component({ selector: 'my-cmp', template: `<p>my-component</p>` }) class mycomponent implements oninit { constructor( private router: router, private myservice: myservice ) { } ngoninit() { console.log('ngoninit'); } }
Comments
Post a Comment