Angular Best Practices
Angular Best Practices
Angular Best Practices
App Module
Core Modules
o Shared Singleton services
o App - Level Components like nav component or search component
Shared Module
o Shared Components
o Directives
o Pipes
Feature Module
o Feature level services
o Components
o Directives & pipes
1. Core Module
Create a core folder with module file like core.mModule is a set of components
App Module
Core Modules
o Shared Singleton services
o App - Level Components like nav component or search component
Shared Module
o Shared Components
o Directives
o Pipes
Feature Module
o Feature level services
o Components
o Directives & pipes
1. Core Module
1. Create a core folder with module file like core.module.ts
2. Import singleton services
3. Import and export core components like nav
4. Create NgModule
a. Add CommonModule from angular core as import
5. Add Core Module to App Module imports
6. Be Careful that any directives used in app module should be imported in core module, ex: router
module
2. Shared Module
1. Create a shared module with module file shared.module.ts
2. Import and export shared components
3. Import Module in App.Module.ts
3. Feature Module
1. Import Common Module (if not exported in shared module) and import router and other modules
2. Import feature module in app.module.ts
1. module.ts
2. Import singleton services
3. Import and export core components like nav
4. Create NgModule
a. Add CommonModule from angular core as import
5. Add Core Module to App Module imports
6. Be Careful that any directives used in app module should be imported in core module, ex: router
module
2. Shared Module
1. Create a shared module with module file shared.module.ts
2. Import and export shared components
3. Import Module in App.Module.ts
3. Feature Module
1. Import Common Module (if not exported in shared module) and import router and other modules
2. Import feature module in app.module.ts
LIFT:
Environment Setup:
Naming Structure:
1. File Naming
a. Components:
i. Components: {descriptor}.component.ts i.e. coupon.component.ts
ii. Component styles:{descriptor}.component.css i.e. coupon.component.css
iii. Component template: {descriptor}.component.html i.e. coupon.component.html
b. Services: {descriptor}.services.ts i.e. coupon.services.ts or data-dashboard.services.ts
c. Note: Replaces spaces with -
2. Folder Structure:
a. Files related to a component should be in a folder ex:
i. Coupon
1. Coupon.component.ts
2. Coupon.component.html
3. Coupon.component.css
4. Coupon.service.ts (if used only in coupon component)
b. Common Components?
3. One Item Per File
a. Each component file or service file should represent only one component/service
b. You don’t want to keep 2 components in one service, for ex: you don’t want to keep coupon &
deals component classes in coupon.component.ts as it would become difficult to find.
o Classes
PascalCase i.e CouponComponent or CouponService (suffix should be maintained)
Make class name consisted with file name
Ex: add-coupon.component.ts should have AddCouponComponent instead
of AddComponent as class name
o Imports
3rd party imports first
Angular/core imports
In house imports next
Our own Services or model imports
Immutability
o Not changing existing objects in memory
o Helps to avoid bugs
o How?
Use Object.assign instead of updating the same object.
Function size
o Break functions to smaller size for easy readability
Performance
Ahead of time compilation
1. Compiles the app and creates a dist folder with files which can be deployed in production
a. Use Angular CLI
b. Use ng build --prod to do a production build