【问题标题】:Ngrx example app: how are states wired togetherNgrx 示例应用程序:状态如何连接在一起
【发布时间】:2018-11-09 06:52:13
【问题描述】:

为了改进我的应用程序状态管理,我查看了 ngrx 演示应用程序。

example-app/app/core/containers/app.component.ts 中,组件包含两个在构造函数中初始化的可观察对象。 observables 被初始化为

constructor(private store: Store<fromRoot.State>) {
    /**
     * Selectors can be applied with the `select` operator which passes the state
     * tree to the provided selector
     */
    this.showSidenav$ = this.store.pipe(select(fromRoot.getShowSidenav));
    this.loggedIn$ = this.store.pipe(select(fromAuth.getLoggedIn));
}

我不明白的是,如何在那里使用 fromAuth.getLoggedIn。注入的存储是从Root.State 键入的。 root-reducer example-app/app/reducers/index.ts 与 auth-state 没有连接。 auth-state example-app/app/auth/reducers/index.ts 确实扩展了 root-state,所以我可以理解这些对 auth-state 的调用是有效的,但我不明白它是如何工作的。

【问题讨论】:

    标签: ngrx ngrx-store


    【解决方案1】:

    该示例使用一个存储,因此所有特征化简器的状态都将存储在根(共享)状态旁边,简而言之,所有化简器和状态都连接到一个单一的来源,即存储。

    查看特性模块文件中的这一行:https://github.com/ngrx/platform/blob/master/example-app/app/auth/auth.module.ts#L36 你可以看到它将特性模块 reducer 导入到名为 auth 的存储中,因此 fromAuth.getLoggedIn 选择了 auth 状态的切片。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-06
      • 2019-12-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 2014-09-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多