ngrx 选择器不返回数字而是返回整个状态对象

问题描述

我正在尝试使此功能选择器正常工作。但是选择器不是返回一个数字而是返回整个状态对象(在我的例子中是 CounterState)

这是我的工作示例:https://stackblitz.com/edit/angular-ivy-ctypd1?file=src%2Fapp%2Fcounter.selectors.ts

我在页面上得到以下输出Current Count: [object Object]

我在控制台中得到以下输出

{counter: 1,loaded: true,loading: false}
{counter: 2,loading: false}
{counter: 3,loading: false}

我只期待 1,2,3 我错过了什么?

问候,

解决方法

这是因为您在注册 reducer 时创建了一个嵌套的树结构。

StoreModule.forFeature(counterFeatureKey,{ counter: counterReducer }),

这应该是(或者需要调整选择器):

StoreModule.forFeature(counterFeatureKey,counterReducer ),