Comments (3)
Rationale
Vs
Redux diagram
from angular-playbook.
Example variations
- Example without external interaction , (.ie, http request) sample code pattern
- Example with external interaction
- The example placed the API request inside of app.component.ts
- Instead of following redux thunk pattern
from angular-playbook.
State level
angular supports generate feature state using cli
In NgRx, StoreModule.forRoot() and StoreModule.forFeature() are methods to configure state management for an Angular application, each serving distinct purposes
-
StoreModule.forRoot()
:-
to set up the root application state. It should be called only once in your application, typically in the
AppModule
. -
configures the global state for your application and initializes the top-level state slices.
-
The first argument to
forRoot()
is the initial state of the application, and the second argument is a configuration object (optional). -
Example usage:
import { StoreModule } from '@ngrx/store'; @NgModule({ imports: [ // ... StoreModule.forRoot({ /* initial state */ }), // ... ], // ... }) export class AppModule { }
-
-
StoreModule.forFeature()
:-
Tto set up feature modules. Feature modules are used to organize the state of specific features or parts of your application.
-
Called once for each feature module and allows you to add additional state slices to your application.
-
Example usage:
import { StoreModule } from '@ngrx/store'; @NgModule({ imports: [ // ... StoreModule.forFeature('featureName', /* feature reducer */), // ... ], // ... }) export class FeatureModule { }
-
In the above example,
'featureName'
is a unique string identifier for this feature module, and/* feature reducer */
is the reducer function for this feature's state slice. -
You can use
createFeatureSelector
andcreateSelector
to access the state of the feature module.
-
-
When to Use Each:
-
forRoot
: Use this in the root module of your application (typicallyAppModule
) to set up the global state for your application. It initializes the top-level state slices and should only be called once. -
forFeature
: Use this in feature modules to add additional state slices. Feature modules are typically used to manage the state for specific parts of your application. You can callforFeature
multiple times to set up state for different features.
-
In summary, forRoot
is used in the root module to configure the global application state, while forFeature
is used in feature modules to manage specific parts of the application's state. This helps in organizing and modularizing the state management in NgRx applications.
from angular-playbook.
Related Issues (20)
- Essentials Of Creating Angular SPA Application HOT 1
- RxJs and Reactive Programming HOT 5
- A guess on Angular Material UI
- Angular Material UI HOT 2
- Ecosystem UI libraries
- Angular Nuance
- [Angular Material UI] Table and its mechanism
- HTTP request and Http Client HOT 1
- CI/CD configuration for Angular SPA Applications
- [Angular Material UI] Build your toast service HOT 2
- [Angular Fundamental] Build Angular Directives HOT 4
- [Angular Fundamental] Reactive Form HOT 7
- Testing Angular Applications HOT 3
- Routing in Angular Applications HOT 4
- State Management In Angular Applications HOT 1
- Rxjs common usage scenarios HOT 1
- UI Effect libraries HOT 2
- Code Style Guide In Angular Application HOT 3
- [Test Angular Application] Unit Testing HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angular-playbook.