My Favorite Tips and Tricks in Angular
1. Use Services to Handle Side Effects
import { Component } from "@angular/core";
@Component({
selector: 'app-component',
template: '<ul> <li *ngFor="let item of items">{{item}}</li> </ul>',
})
export class AppComponent implements OnInit{
constructor(private http: HttpClient){
}
items = [];
getItems(){
return this.http.get('http://server.com/items')
}
ngOnInit(){
this.getItems.subscribe(items => this.items = items);
}
}2. ng add
3. Web Components
4. Aliases for Import Statements
5. Safe Navigation Operator for String Interpolation
6. Handle Errors Properly with an Error Handler
7. Lazy Load Non-Vital Components
8. Define form parameters before Component definition
9. Use renderer addClass, removeClass where possible. It will prevent too many change detection checks.
10. Try to use get, set on Input() instead ngOnChanges. When you have many Inputs in the ngOnChanges, each “if” has to be checked.
11. Use pipe when rendering content in ngFor
12. Add baseUrl and path to your compilerOptions to avoid any inconsistency when importing other files
13. Add stylePreprocessorOptions to your angular.json file to avoid inconsistency while importing other files
14. Run npm outdated command or add npm-check once a month to keep your dependencies updated. It will definitely help you keep track of changes. It’s much easier to update Angular 5 to 6 than 4 to 6.
npm outdated command or add npm-check once a month to keep your dependencies updated. It will definitely help you keep track of changes. It’s much easier to update Angular 5 to 6 than 4 to 6.15. Run npm audit command once a month to check if any of the libraries has any vulnerabilities. It will help you keep your app secure.
16. Use parent in form validation instead of this.form which may not be initialised while doing/performing custom validation check.
17. Keep route names as const. It will prevent accidental typos.
18. Start using webpack-bundle-analyzer. It will help you detect any fast-growing modules.
19. Use browser Performance tests. 17ms rendering time means you use 60fps. Actions with fewer than 60fps and more than 30fps are ok. Any speed below 30fps makes the user notice the visualized slowdown of the application.

20. Use Augury chrome extension to track the current state of components.
21. Prettier as code formatter
22. declare keyword – create a custom type when the native one doesn’t exist. It’s great for typing in if the JS libraries hasn’t been typed.
23. Declare dictionary key and value types arg: { [key: string]: number }. Each value of this object will be typed as a number
24. Ampersand operator:
25. Declare tuple types
26. Big figures You can use _ as a digit separator to make big figures more readable
27. Abstract class
28. Reduce the number of any types:
29. Add no-string-literal to tsconfig – disallowed to access key by obj[‘key’], only obj.key allowed.
30. Force generic types:
31. Reduce code complexity
32. Keep your codebase clean and dry:
33. Add “noImpicitAny”: true to tsconfig – it will throw a compilation error when the types cannot be inferred or inferring them might result in unexpected errors
34. Add “noImplicitReturns”: true to tsconfig – it will throw a compilation error when you try to return different types in each if statement
35. Add “strictFunctionTypes”: true to tsconfig – it will throw a compilation error when an incorrect parameter is applied to the function.
36. Add “noUnusedParameters”: true to tsconfig – it will throw a compilation error when an unused function parameter is detected. Arguments started with underscore are allowed.
37. Add “noUnusedLocals”: true to tsconfig – it will throw a compilation error when unused variables or imports are found in the code.
Last updated
