Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Implementation best practices

Victor Dias edited this page Jul 16, 2018 · 6 revisions

Naming services

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)

Constructor vs ngOnInit

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:

Mock: mimic an Observable

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 }, ...