Issue
Current version of Angular and ngrx are 16.0.0.
StoreModule.forRoot({
app: appReducer,
anotherFeatureName: anotherFeatureReducer
}),
EffectsModule.forRoot([
AppEffects,
AnotherEffects
]),
I started migration to standalone Angular app. So I need to get rid off StoreModule and EffectsModule from app.module.ts. But I can find the way to provide several reducers and relative effects using standalone API's.
Tried different ways to provide but no ones is working as expected. The state is undefined when app tries to read it.
Solution
1. First, make sure you have imported the necessary NgRx modules and your reducers and effects.
import { provideState } from '@ngrx/store';
import { provideEffects } from '@ngrx/effects';
import { appReducer } from './app.reducer';
import { anotherFeatureReducer } from './another-feature.reducer';
import { AppEffects } from './app.effects';
import { AnotherEffects } from './another.effects';
2. In your app module, use the provideState() function to provide your root reducers
import { provideState } from '@ngrx/store';
@NgModule({
imports: [
//Remove StoreModule.forRoot()
//Remove EffectsModule.forRoot()
// Add provideState() to provide your root reducers
provideState({
app: appReducer,
anotherFeatureName: anotherFeatureReducer,
}),
],
})
export class AppModule {}
3. Similarly, use the provideEffects() function to provide your root effects
import { provideEffects } from '@ngrx/effects';
@NgModule({
imports: [
//Remove StoreModule.forRoot()
//Remove EffectsModule.forRoot()
// Add provideState() to provide your root reducers
provideState({
app: appReducer,
anotherFeatureName: anotherFeatureReducer,
}),
// Add provideEffects() to provide your root effects
provideEffects([AppEffects, AnotherEffects]),
],
})
export class AppModule {}
Answered By - 봊Ì'ɱ Јìԥ
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.