This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Implementation best practices
Victor Dias edited this page Jul 16, 2018
·
6 revisions
import { AnalyticsService } from '@shared/analytics.service';
import { AuthService } from '@providers/auth.service';
constructor ItemsComponent (
analyticsService: AnalyticsService,
authService: AuthService
) {}
[JavaScript ES6+: var, let, or const?](a single identifier should only be used to represent a single concept)
ngOnInit is part of Angular lifecycle, while constructor is part of ES6 JavaScript class. Mostly we use ngOnInit for all the initialization/declaration and avoid stuff to work in the constructor. The constructor should only be used to initialize class members but shouldn't do actual "work". So you should use constructor() to setup Dependency Injection and not much else.
Sources:
- https://stackoverflow.com/questions/35763730/difference-between-constructor-and-ngoninit/35763811#35763811
- https://blog.angularindepth.com/the-essential-difference-between-constructor-and-ngoninit-in-angular-c9930c209a42
Observable.of from RxJS to give an Observable stream from the results, this is a nice way to mimic an Observable response. => Could be use to mock contacts or comments.
this.contacts = Observable.of([ { "id": 1, "name": "Laura", "email": "[email protected]", "age": 47 }, ...