2

我已经努力了 2 天来编写 featureA(“场景”)模块和嵌套的 featureB(“意图”)模块的状态。

这是我的状态所需的结构(结构 1):

{
  authentication: { ... },
  router: { ... },
  scenarios: {
    resources: { ids: { ... }, entities: { ... } },
    intents: {
      resources: { ids: { ... }, entities: { ... } }
    }
  }
}

我也很高兴(结构2):

{
  authentication: { ... },
  router: { ... },
  scenarios: { ids: { ... }, entities: { ... } },
  intents: { ids: { ... }, entities: { ... } },
}

虽然它不反映我的模块的结构。

结构 1 的问题是scenarios/reducers/index.ts暴露了 a ActionReducerMapscenarios/modules/intents/reducers/index.ts也暴露了 a ActionReducerMap,我不知道如何组合它们。由于类型冲突,我尝试的所有内容甚至都无法编译。

结构 2 的问题在于,由于在第一页加载时没有加载,并且应用程序的其他 reducer 正在执行它们的工作intents,因此状态的一部分一直被退出状态。IntentsModule

这是代码:

/scenarios/scenarios.module.ts

import { reducers, getInitialState } from './reducers';

@NgModule({
  imports: [
    ...,

    ScenariosRoutingModule,

    StoreModule.forFeature('scenarios', reducers, { initialState: getInitialState }),
    EffectsModule.forFeature([ScenariosEffects]),
  ],
})
export class ScenariosModule {}

/scenarios/reducers/index.ts

import * as fromRoot from '../../../app.reducers';
import * as fromScenarios from './scenarios.reducers';
import * as fromIntents from '../modules/intents/reducers';

export interface ScenariosState {
  resources: fromScenarios.State;
  intents: fromIntents.IntentsState;
}

export interface State extends fromRoot.State {
  scenarios: ScenariosState;
}

export const reducers: ActionReducerMap<ScenariosState> = {
  resources: fromScenarios.reducer,
  intents: fromIntents.reducers, <-- This is the part with types conflicts
};

/scenarios/modules/intents/intents.module

import { reducers, getInitialState } from './reducers';

@NgModule({
  imports: [
    ...,

    IntentsRoutingModule,

    StoreModule.forFeature('intents', reducers, { initialState: getInitialState }), <-- Also this part seems to add 'intents' as a top-level property of state
    EffectsModule.forFeature([IntentsEffects]),
  ],
})
export class IntentsModule {}

/scenarios/modules/intents/reducers/index.ts

import * as fromIntents from './intents.reducers';

export interface IntentsState {
  resources: fromIntents.State;
}

export interface State {
  intents: IntentsState;
}

export const reducers: ActionReducerMap<IntentsState> = {
  resources: fromIntents.reducer,
};

export function getInitialState(): IntentsState {
  return {
    resources: fromIntents.initialState,
  };
}
4

2 回答 2

2
  • importexportstructure 2 IntentsModule中的AppModule
  • import在根ScenariosModule之后。IntentsModuleAppModule

嵌套各种IntentsModulesScenariosModules。相反,尽可能地创建分离和扁平化您的项目结构,这是structure 2.

我发现,除了 Auth 相关功能之外,大多数情况下的一个好方法是为每个功能创建两个模块以延迟加载,同时将状态管理与用户界面分离,但这些相应的模块应该共享一个API相同的模块形状即:

  • (FrameworkLayoutModuleFrameworkStoreModule) 和 ( AuthModule)。

随后,可重用特征可以根据需要由 aSecureModule或 a延迟加载PublicModule

于 2018-08-10T01:44:32.883 回答
0

在两个模块中使用“意图”存储的这个问题。并且 NgRx 不能两次创建“意图”存储。

解决方案 1

如果不在那里使用,从场景.模块或意图.模块中删除“意图”。

解决方案 2

将“意图”存储移动到 StoreModule.forRoot 或场景和意图父模块中。

于 2018-08-09T11:12:03.573 回答