有没有办法使用provideState()提供多个reducer作为根reducer

问题描述 投票:0回答:1

Angular 和 ngrx 的当前版本是 16.0.0。

StoreModule.forRoot({
      app: appReducer,
      anotherFeatureName: anotherFeatureReducer
    }),
    EffectsModule.forRoot([
      AppEffects,
      AnotherEffects
    ]),

我开始迁移到独立的 Angular 应用程序。所以我需要从 app.module.ts 中删除 StoreModule 和 EffectsModule。但我可以找到使用独立 API 提供多个减速器和相关效果的方法。

尝试了不同的方式来提供,但没有一个能按预期工作。当应用程序尝试读取状态时,状态未定义。

angular typescript ngrx
1个回答
0
投票

1。首先,确保您已导入必要的 NgRx 模块以及您的减速器和效果。

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。在您的应用程序模块中,使用 ProvideState() 函数来提供您的根减速器

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.同样,使用 ProvideEffects() 函数来提供您的根效果

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 {}
© www.soinside.com 2019 - 2024. All rights reserved.