如何从子模块访问 angular-redux 存储?

Gre*_*nko 3 javascript angular-module angular angular-redux

在我的 angular 应用程序中,我使用 angular-redux 进行应用程序状态管理。在我的主模块中,我定义了我的 redux 存储。像这样:

export class MainModule {
  constructor(private ngRedux: NgRedux<MainAppState>,
              private devTools: DevToolsExtension) {
    let enhancers = [];

    if (environment.production === false && devTools.isEnabled()) {
      enhancers = [...enhancers, devTools.enhancer()];
    }

    this.ngRedux.configureStore(
      reducer,
      {} as MainAppState,
      [],
      enhancers);
  }
}
Run Code Online (Sandbox Code Playgroud)

我创建了新的子模块,其中包含一些组件。这些组件应该访问应用程序状态。在这些组件之一中,我通过 @select 访问以进行存储,但这不起作用。这是我访问商店的方式:

export function getLanguage(state: LanguageState) { return state.userLanguage; }
Run Code Online (Sandbox Code Playgroud)

我在我的 ChildComponent 类中有这个代码:

export class ChildComponent implements OnInit {

  @select(getLanguage) savedUserLanguage$: Observable<LanguageState>;

  // more code

}
Run Code Online (Sandbox Code Playgroud)

如何从子模块访问应用程序状态存储?我应该在子模块中导入什么?只为 redux store 处理创建自己的模块会更好吗?也许我忘记了什么?

我使用 Angular v4 和 @angular-redux/store v6。

小智 5

我建议创建一个单独的模块,其中只包含您的商店,例如StoreModule. 然后,您可以将您的StoreModule模块导入所有子模块并从那里访问您的商店。这是他们在官方示例应用程序中的方式:

StoreModule: https : //github.com/angular-redux/example-app/blob/master/src/app/store/module.ts

子模块: https : //github.com/angular-redux/example-app/blob/master/src/app/elephants/module.ts

子模块中的组件: https : //github.com/angular-redux/example-app/blob/master/src/app/elephants/page.ts