NGRX | store.select(state => state.MyObject)|返回undefined

时间:2018-06-08 16:38:06

标签: javascript angular typescript rxjs ngrx

情况,我尝试使用以下方法检索状态信息:

listObject: Array<ListObject>;
this.list$ = this.store.select(state => state.MyListObject);
this.listSub$ = this.list$.subscribe(list => this.listObject = list);

除了我几天前创建的新组件之外,这适用于每个组件。 listSub $ subscription返回一个未定义的对象。踢球者是我可以通过Redux DevTools查看状态。在状态中,MyListObject中有两个对象。在登录事件中,调度存储以加载列表;我可以看到Redux DevTools中的列表加载以及日志记录。

我无法弄清楚为什么this.store.select(state =&gt; state.MyListObject)在状态存在时返回undefined。有没有人见过这个问题?

我确保StoreModule.forRoot({})引用了我的reducer,我使用的效果在EffectsModule.forRoot([])中。

问题代码

clatschList: ClatschList;
this.clatschList$ = this.store.select(state => state.ClatschList);
this.ClatschListSub = this.clatschList$.subscribe(list => this.clatschList = list);

MyListObject

export class ClatschList {
  public clatsches: Array<ClatschSummary> = [];
  public links: Array<Link> = [];
}

应用状态

export interface ClatschesAppState {
  ClatschList: ClatschList;
 }

动作

export type Action
  = LoadMemberClatsches
| LoadMemberClatschesSuccess;

export const LOAD_MEMBER_CLATCHES = 'LOAD_MEMBER_CLATCHES';
export class LoadMemberClatsches {
  readonly type = LOAD_MEMBER_CLATCHES;
  constructor() {}
}

export const LOAD_MEMBER_CLATCHES_SUCCESS = 'LOAD_MEMBER_CLATCHES_SUCCESS';
export class LoadMemberClatschesSuccess {
  readonly type = LOAD_MEMBER_CLATCHES_SUCCESS;
  constructor(public clatschList: ClatschList) {}
}

减速

export function MemberClatschesReducer(state: ClatschList = new 
ClatschList(), action: ClatschAction.Action) {

  switch (action.type) {
    case ClatschAction.LOAD_MEMBER_CLATCHES_SUCCESS: {
      return action.clatschList;
    }

    default: {
      return state;
    }
  }
}

效果

  @Effect() LoadMemberClatschList$ = this.actions$
      .ofType(ClatschAction.LOAD_MEMBER_CLATCHES).pipe(
        switchMap((action: LoadMemberClatsches) =>
          this.clatschService.FindAllByMember().pipe(
            map(list => new ClatschAction.LoadMemberClatschesSuccess(list)))
        )
      );

模块

@NgModule({
  imports: [
    CommonModule,
    HttpClientModule,
    AuthorizationModule,
    StoreModule.forRoot(
               {  makerContext: makerContextReducer,
                  makers: makerReducer,
                  locations: locationsReducer,
                  locationSearchResult: locationSearchResultReducer,
                  locationSearchResultDetail: locationSearchResultDetailReducer,
                  events: MakerEventsReducer,
                  eventContext: EventContextReducer,
                  EventList: EventListReducer,
                  EventListPagination: EventListPaginationReducer,
                  SelectedEvent: SelectedEventReducer,
                  MemberClatschList: MemberClatschesReducer,
        }),
    EffectsModule.forRoot( [MakerEffect, LocationEffect, EventEffect, 
ClatschEffect]),
    StoreDevtoolsModule.instrument()
  ],
  declarations: [LocationSearchComponent],
  providers: [ MakerService,
    StorageService,
    EventService,
    LocationService,
    ClatschRepositoryService,
    ClatschService,
    ApiResourceService,
    LoggingService,
    UserNotificationService,
    HttpErrorHandlerService ]
})

export class CoreModule { }

1 个答案:

答案 0 :(得分:0)

好的,谢谢您发布其余的代码。我找到了你的问题。在模块中,您声明了:

StoreModule.forRoot({  
    ...
    MemberClatschList: MemberClatschesReducer,
})

这意味着您的应用状态将具有属性MemberClatschList,该属性将由MemberClatschesReducer处理,但在AppState界面和您调用属性ClatschList的组件中都有。它始终返回undefined,因为您尝试访问state.ClatschList,而实际上它应该是state.MemberClatschList