Skip to content

Commit d604695

Browse files
docs: add guides for standalone APIs in module-based apps (#3854)
1 parent 2d1669e commit d604695

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

projects/ngrx.io/content/guide/effects/index.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,32 @@ The `EffectsModule.forFeature()` method or `provideEffects()` function must be a
346346

347347
</div>
348348

349+
## Standalone API in module-based apps
350+
351+
If you have a module-based Angular application, you can still use standalone components. NgRx standalone APIs support this workflow as well.
352+
353+
For module-based apps, you have the `EffectsModule.forRoot([...])` included in the `imports` array of your `AppModule`, which registers the root effects for dependency injection. For a standalone component with feature state/effects registered in its route configuration to successfully run effects, you will need to use the `provideEffects([...])` function in the `providers` array of your `AppModule` to register the injection token. For module-based with standalone components, you will simply have both.
354+
355+
<code-example header="app.module.ts">
356+
import { NgModule } from '@angular/core';
357+
import { EffectsModule, provideEffects } from '@ngrx/effects';
358+
359+
import { MoviesEffects } from './effects/movies.effects';
360+
import * as actorsEffects from './effects/actors.effects';
361+
362+
@NgModule({
363+
imports: [
364+
EffectsModule.forRoot(MoviesEffects, actorsEffects),
365+
],
366+
providers: [
367+
provideEffects(MoviesEffects, actorsEffects)
368+
]
369+
})
370+
export class AppModule {}
371+
</code-example>
372+
373+
<div class="alert is-critical">
374+
349375
## Incorporating State
350376

351377
If additional metadata is needed to perform an effect besides the initiating action's `type`, we should rely on passed metadata from an action creator's `props` method.

projects/ngrx.io/content/guide/store/reducers.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,30 @@ After the feature is loaded, the `game` key becomes a property in the object and
272272
```
273273

274274
Whether your feature states are loaded eagerly or lazily depends on the needs of your application. You use feature states to build up your state object over time and through different feature areas.
275+
276+
## Standalone API in module-based apps
277+
278+
If you have a module-based Angular application, you can still use standalone components. NgRx standalone APIs support this workflow as well.
279+
280+
For module-based apps, you have the `StoreModule.forRoot({...})` included in the `imports` array of your `AppModule`, which registers the root store for dependency injection. Standalone components look for a different injection token that can only be provided by the `provideStore({...})` function detailed above. In order to use NgRx in a standalone component, you must first add the `provideStore({...})` function the the `providers` array in your `AppModule` with the same configuration you have inside of your `forRoot({...})`. For module-based apps with standalone components, you will simply have both.
281+
282+
<code-example header="app.module.ts">
283+
import { NgModule } from '@angular/core';
284+
import { StoreModule, provideStore } from '@ngrx/store';
285+
import { scoreboardReducer } from './reducers/scoreboard.reducer';
286+
287+
@NgModule({
288+
imports: [
289+
StoreModule.forRoot({ game: scoreboardReducer })
290+
],
291+
providers: [
292+
provideStore({ game: scoreboardReducer })
293+
]
294+
})
295+
export class AppModule {}
296+
</code-example>
297+
298+
Note: Similarly, if you are using effects, you will need to register both `EffectsModule.forRoot([...])` and `provideEffects([...])`. For more info, see [Effects](guide/effects).
275299

276300
## Next Steps
277301

0 commit comments

Comments
 (0)