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

How can I access angular-redux store from child module?

在我的 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);
  }
}

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

export function getLanguage(state: LanguageState) { return state.userLanguage; }

我的 ChildComponent 中有这段代码 class:

export class ChildComponent implements OnInit {

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

  // more code

}

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

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

我建议创建一个仅包含您的商店的单独模块,例如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

我正在考虑将一些使用原型继承的丑陋旧 JavaScript 代码重构到 Angular 7+ 项目中。我几乎在问自己同样的问题。受我的 udemy Angular 课程的启发,我尝试使用 ngrx 存储和延迟加载模块进行实验。

(请记住,ngrx 类似于@angular-redux,但它们不是一回事。有关详细信息,请参阅 https://ngrx.io/docs。)

Here it is.

我在主模块中使用 StoreModule.forRoot 创建商店,在每个延迟加载模块中,我使用 StoreModule.forFeature 创建对商店的引用。

(详见https://ngrx.io/api/store/StoreModule。)

当我使用延迟加载的组件在商店上发送操作时,这些操作(和相应的缩减程序)似乎会更改主应用程序组件订阅的值。

此外,当我使用主应用程序组件在商店上发送操作时,这些操作(和相应的缩减程序)似乎会更改延迟加载组件订阅的值。

此外,很难在一个简单的 200-500 个字符块中解释我所做的事情,所以我不得不使用 github 项目。