选择器没有获得任何角度(ngrx)状态

时间:2018-12-26 12:04:04

标签: angular redux ngrx

你好,当我初始化我的应用程序并运行它时,我的选择器没有收到任何状态。无论如何,我已经设置了默认的初始状态,并将其传递给我的reducer,因为它们是在应用程序初始化时订阅存储的第一件事,并且它们提供初始状态,但是我的选择器无法接收任何状态。另外,我已经使用StoreModule.forFeature(GlobalState.stateName, globalReducer)将状态连接到我的reducer,甚至使用了ngrx devtools,但是要跟踪状态,但是初始状态仍然是空的,我真的耗尽了为什么会发生这种行为的想法

我尝试使用devtools跟踪状态,尝试调试选择器并检查何时接收到新值,但这没有发生。

操作:

import { Action } from '@ngrx/store';
import { HttpErrorResponse } from '@angular/common/http';

export const GlobalActionTypes = {
  HandleError: '[HttpResponseError] This will handle errors from serivces',
  LoadingScreen: '[Loading] This will show loading screen'
}

export class HandleErrorAction implements Action {
  readonly type: string = GlobalActionTypes.HandleError;
  constructor(public payload: HttpErrorResponse) { }
}

export class LoadingScreenAction implements Action {
  readonly type: string = GlobalActionTypes.LoadingScreen;
  constructor(public payload: boolean) { }
}

减速器:

import { Action } from '@ngrx/store';
import { GlobalActionTypes, LoadingScreenAction } from 
'../actions/global.actions';
import { GlobalState } from '../state/global.state';
import { ActionsMap } from 'src/app/core/models/reducer.model';

const initialState: GlobalState = {
  isLoading: false,
}

function loadingScreen(
  state: GlobalState,
  action: LoadingScreenAction): GlobalState {
    const newState = Object.assign({}, state);
    newState.isLoading = action.payload;
    return newState;
}

export const mapToReducer: ActionsMap<GlobalState> = {
  [GlobalActionTypes.LoadingScreen]: loadingScreen
}

export function globalReducer
  (state: GlobalState = initialState, action: Action) {

  mapToReducer[action.type] ? mapToReducer[action.type](state, action) : state
}

状态:

export class GlobalState {
  static stateName: string = 'Global State'
  isLoading: boolean;
}

选择器:

import { createFeatureSelector, createSelector } from '@ngrx/store';
import { GlobalState } from '../state/global.state';

export const featureSelector = createFeatureSelector<GlobalState>
  (GlobalState.stateName);

export const loadingScreenSelector = createSelector(
  featureSelector,
  state => state.isLoading
);

AppModule imports: [
    StoreModule.forRoot({}),
    StoreModule.forFeature(GlobalState.stateName, globalReducer),
]

我有一项服务,该服务过去通常将动作分派为true和false。然后,我使用HTTP_INTERCEPTOR并听取请求。每当出现新请求时,我都会使用service来调度有效负载为true的动作。当请求是成功或否的时候,我调度的负载为false的动作。当我检查newState的有效负载时,我什至进入了reducer中,动作被调度了。但是,当状态发生变化时,选择器不会收到任何信息。我真的尝试了一切,但我的构想耗尽了。欢迎任何建议。 :(

0 个答案:

没有答案