visit
Also Read:- Is Angular Worth Learning in 2022?
Extended Edition
1. Create an Angular Application
Ng new your -project-name
Ng g module mall -- routing
Ng g component mall/products
2. Run the App
Cd angular-state-management
3. Install Ngrx and tools
npm install @ngrx/store - - save
npm install @ngrx/effects - - save
npm install @ngrx/data - - save
npm install @ngrx/entity - - save
npm install @ngrx/store-dev tools - - save
4. Add NgRx Store to the Application
ng generate @ngrx schematics: store state - - root - - module app.module.ts
5. Create a Sub Module
Then, create a separate module for customersng generate module customer
6. Create Customer Model
ReactJs development has professionals who create customer models and add codes for their client’s Angular application.
ng generate class models/customer
7. Add Actions
We have to manage the collection of customers. We can change the collection of the customer's state by using the actions. We will add Ngrx related code.
ng generate action custome .store/action/customer
8. Add a customer reducer
All state changes will occur inside the reducer based on the selected action. The reducer will return to the newly created customer list object every time the state changes. Then, generate reducers for customers. Don’t add failures and success actions to the reducer.
CREATE src/app/ngrx/customer.reducer.spec.ts
CREATE src/app/ngrx/customer.reducer.ts
9. Add Selector
Make a selector get the customers from the store. Then, modify the codeng generate selector customer/store/selector/customer
10. Add UI Component for Customers
Make customer view componentng generate component customer/customer view
<h4>List of customers< /h4>
<div *ngFor=”let customer of customers$ | async; let i = index’ >
<span >{ {i+1} }.</span> { {customer.name} }
</div>
11. Add UI Controls to Add New Customers
Make UI control component to add customersng generate component customer/customer Add
This. store.dispatch (add customer(customer));
12. Add Store Module In Customer Module
StoreModule.for Feature(customer Feature Key, reducer)
,
Exports: [ customer view component, customer Add component ]
Conclusion
State management in Angular provides a way to store data and share it between components. You can contact ReactJs developers for state management in Angular applications using Ngrx. You can use Ngrx to reduce the code in your angular application. The data is also easily accessible and can be found anywhere in the application.